Tutorial :How to find the latter half of a pattern?



Question:

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)


Solution:1

(^|\s)foo/([a-zA-Z]+)  


Solution:2

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)  


Solution:3

\b is a word boundary, ^ is a start of line marker

^foo/(\w+)  


Solution:4

How about

^(foo)/([a-zA-Z]+)  

or

(?<!http://)(foo)/([a-zA-Z]+)  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »