Wednesday 25 October 2017

java - regex for matching something if it is not preceded by something else

itemprop="text">


So with regex in java, I
want to write a regex that will match if and only if the pattern is not preceded by
certain characters. For
example:



String s = "foobar barbar
beachbar crowbar bar
";


I want to match if
bar is not preceded by foo. So output would
be:



barbar
beachbar
crowbar

bar


I
know this is probably a very simple question. I am trying to learn regex, but in the
meantime I need something to work now.


class="post-text" itemprop="text">
class="normal">Answer



You want
to use negative lookbehind like this:



\w*(?



Where
(? means "only if it doesn't have "x" before this
point".



See href="http://www.regular-expressions.info/lookaround.html" rel="noreferrer">Regular
Expressions - Lookaround for more
information.



Edit:
added the \w* to capture the characters before (e.g.
"beach").


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...