im new to Regular Expressions in general and I start to read more about them , so be gentle 🙂
I want to find all words that begins with my(" or my('. The word itself can contain underscores, characters, digits, basically any char. But it should end with ") or ').
So I tried the following:
Pattern.compile("_(\"(.*)\")"); // for underscores first, instead of my
and
Pattern.compile("(my)(\"(.*)\")");
But this give me other things back as well, and I can’t see why and where I making the thinking mistake…
Thanks
If you want to match
my("xxx")andmy('xxx')but notmy("xxx')then try the following expression:Here’s a short breakdown of the expression:
my\(...\)means the match should start withmy(and end with)(?:"[^"]*"|'[^']*')means a sequence of characters surrounded by either double quotes or single quotes (therefore the character class means “any character not being a double quote” or “any character not being a single quote”)Edit:
The problem with the expression
(my)("(.*)")is, that it is greedy and the match would start atmy("but end on the last")due to the.*which matches anything. Thus it would matchmy("xxx") your("yyy")because.*matchesxxx") your("yyy.For more information on regular expressions see http://www.regular-expressions.info