How to find the latter half of a pattern?
I'm trying to find the latter half of patterns of the following template:
foo/BAR
'BAR' is the one I'm trying to retrieve. I tried with something like:
\b(foo)/([a-zA-Z]+)
This works fine, but this also matches http://foo/BAR
- which I don't want.
I also tried
\\s(foo)/([a-zA-Z]+)
but this doesnt match when the line starts with foo/BAR. (I'm using java.util.regex)
(^|\s)foo/([a-zA-Z]+)
If you define a full "foo/BAR" token as both preceeded and followed by whitespace (or begin/end of the line)
I.e. it would find "abc", "XyZ", and "def" in
"foo/abc 123 hhh foo/XyZ http://foo/BAR foo foo/ foo/ghi% foo/def"
then you want
(?:^|\s)foo/([a-zA-Z]+)(?:$|\s)
\b is a word boundary, ^ is a start of line marker
^foo/(\w+)
How about
^(foo)/([a-zA-Z]+)
or
(?<!http://)(foo)/([a-zA-Z]+)
精彩评论