#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    496
    Rep Power
    89

    Capture up to the first slash [WAS: Simple regex question]


    This shouldn't be difficult for regex masters. I have a text like this:

    folder1/folder2

    Or simply:

    folder1

    I need a regexp that would match "folder1" in both cases. There can be nothing after "folder1" or there can be a slash and some other text that i need to discard. While writing this post, i've figured i could do /^([a-zA-Z]+).*$/, but i'd still like to know if there's any other way to do this (like some sort of an OR operator). I know there should be some combination of | and (), but i just don't know how i'd use that.
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    I'd do:
    Code:
    /^([^\/]+)/
    i.e. capture all characters from the start of the string that aren't forward slashes.

    Comments on this post

    • murklys agrees : Also a good solution

IMN logo majestic logo threadwatch logo seochat tools logo