Java Regex: find a word that matches the beginning and end

im new for regular expressions in general, and I'm starting to read more about them, so be careful :-)

I want to find all words starting with my("or my('. The word itself can contain underscores, characters, numbers, basically any char. But it should end in ")or ').

So, I tried the following:

Pattern.compile("_(\"(.*)\")"); // for underscores first, instead of my

and

Pattern.compile("(my)(\"(.*)\")");

But it also gives me other things, and I cannot understand why and where I am making a thinking mistake ...

thank

+3
source share
3 answers

If you want to combine my("xxx")and my('xxx'), but not my("xxx'), try the following expression:

my\((?:"[^"]*"|'[^']*')\)

Here's a short breakdown of the expression:

  • my\(...\) , my( )
  • (?:"[^"]*"|'[^']*') , , ( " , ", " , " )

Edit:

(my)("(.*)") , my(", ") - .*, -. , my("xxx") your("yyy"), .* xxx") your("yyy.

. http://www.regular-expressions.info

+2

(( )) , . : _\\(\"(.*)\"\\). , , my( "). : ^my\\([\"'](.*)[\"']\\)$. , my(" my("' ") ').

^ $ . ^ , $ . , : foo my('...') bar, my("...") bar ..

, , my("...') my('...").

0

Use the word restriction option,

\bmy\((["']).*?\1\)(?:\b|$)
0
source

All Articles