Skip to content

Instantly share code, notes, and snippets.

@wbailey
Last active December 27, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wbailey/7363555 to your computer and use it in GitHub Desktop.
Save wbailey/7363555 to your computer and use it in GitHub Desktop.
simple regex for jason
1.9.3-p392 :026 > s
=> "DB2:/XMETA/NODE0000/DB2LOG"
1.9.3-p392 :028 > /^([^\/]*\/[^\/]*\/).*$/.match(s)
=> #<MatchData "DB2:/XMETA/NODE0000/DB2LOG" 1:"DB2:/XMETA/">
1.9.3-p392 :030 > /^([^\/]*\/[^\/]*)\/.*$/.match(s)
=> #<MatchData "DB2:/XMETA/NODE0000/DB2LOG" 1:"DB2:/XMETA">
@wbailey
Copy link
Author

wbailey commented Nov 7, 2013

I'll break the regex (contained in line 3 from / ... /) by parts

  1. ^ - indicates beginning of the string
  2. ( - Start a pattern match to capture the string you want
  3. [^\/]* - indicates match all strings that are not a /
  4. / - The first slash in your string
  5. [^\/]* - Repeat again to skip the next string between the slashes
  6. / - The last slash you care about preserving
  7. ) - Close out your pattern match
    6 .* - match and ignore remaining characters
    8 $ - end of string delimeter

The match data just shows the full string and the corresponding text you want to match. If you want to eliminate the trailing slash, then just move the paren to the position in line 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment