简体   繁体   English

正则表达式不止一次出现一个符号

[英]Regex more than one occurrence of a symbol

My regex is as follows (.*?)add\\(\\${2,}(.*?)\\)我的正则表达式如下(.*?)add\\(\\${2,}(.*?)\\)

I want this to validate add($arg1, $agr2)我希望这个验证 add($arg1, $agr2)

That is to detect more than 1 occurance of '$' within the add function.即在 add 函数中检测超过 1 次 '$' 的出现。 I want to return all the text before the add function hence i used (.*?) But the {2,} part doesn't seem to work for me.我想在 add 函数之前返回所有文本,因此我使用了(.*?)但是{2,}部分似乎对我不起作用。

You can use the capturing group to match as least as possible before add.在添加之前,您可以使用捕获组尽可能地匹配。

Then match a single argument starting with a dollar sign and match 1 or more word characters followed by repeating 1 or more times a comma and again an argument.然后匹配以美元符号开头的单个参数并匹配 1 个或多个单词字符,然后重复 1 次或多次逗号,然后再匹配一个参数。

Note that (.*?) will also match an empty string.请注意, (.*?)也将匹配一个空字符串。

(.*?)\badd\(\$\w+(?:\h*,\h*\$\w+)+\)

Explanation解释

  • (.*?) Capture group 1, match any char except a newline 0+ times as least as possible (.*?)捕获组 1,尽可能匹配除换行符以外的任何字符 0+ 次
  • \\badd A word boundary, then match add \\badd一个词边界,然后匹配add
  • \\( Match ( \\(匹配(
  • \\$\\w+ match $ and 1+ word characters \\$\\w+匹配$和 1+ 个单词字符
  • (?: Non capture group (?:非捕获组
    • \\h*,\\h*\\$\\w+ match a comma between optional horizontal whitespace chars \\h*,\\h*\\$\\w+匹配可选水平空白字符之间的逗号
  • )+ Close group and repeat 1+ times )+关闭组并重复 1+ 次
  • \\) Match ) \\)匹配)

See a regex demo查看正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM