3

我一般是正则表达式的新手,我开始阅读更多关于它们的信息,所以要温柔:-)

我想找到所有以my("or开头的单词my('。单词本身可以包含下划线、字符、数字,基本上是任何字符。但它应该以")or结尾')

所以我尝试了以下方法:

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

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

但这也给了我其他的东西,我看不出我为什么以及在哪里犯了这个思维错误......

谢谢

4

3 回答 3

2

如果您想匹配但不匹配my("xxx"),请尝试以下表达式:my('xxx')my("xxx')

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

下面是表达式的简短分解:

  • my\(...\) 表示比赛应该my()
  • (?:"[^"]*"|'[^']*')表示被双引号或单引号包围的字符序列(因此字符类表示“任何不是双引号的字符”或“任何不是单引号的字符”)

编辑:

表达式的问题(my)("(.*)")是,它是贪婪的,并且由于匹配任何东西,匹配将从最后一个开始my("但在最后一个结束。因此它会匹配,因为匹配。").*my("xxx") your("yyy").*xxx") your("yyy

有关正则表达式的更多信息,请参见http://www.regular-expressions.info

于 2012-05-18T09:01:29.190 回答
0

In regular expressions, the brackets (( and )) are actually reserved characters so you will need to escape those. So this regex should do the trick: _\\(\"(.*)\"\\). However, you also stated that you wanted to find words which must begin with my( and must end with "). So you will need to add anchors like so: ^my\\([\"'](.*)[\"']\\)$. This should match any string which starts with my(" or my("' and ends with ") or ').

The ^ and $ are anchors. The ^ will instruct the regex engine to start matching from the beginning of the string and the $ will instruct the regex engine to stop matching at the end of the string. If you remove these anchors, the following would be considered as matches: foo my('...') bar, my("...") bar, etc.

This however will make no distinction and will match also strings like my("...') and my('...").

于 2012-05-18T09:03:01.170 回答
0

使用单词边界选项,

\bmy\((["']).*?\1\)(?:\b|$)
于 2012-05-18T09:20:06.860 回答