简体   繁体   English

正则表达式用于符号之间的单词

[英]Regex for word between symbols

Does someone knows how to make a regex that looks for a word between two instances of the same symbol? 有人知道如何制作一个正则表达式来查找同一符号的两个实例之间的单词吗?

For example, in "gaag a gaga $fafaga$afa" , I need "fafaga", between the two "$" symbols. 例如,在"gaag a gaga $fafaga$afa" ,我需要在两个“ $”符号之间使用“ fafaga”。

You can use following regex if you just want one match : 如果只需要一个匹配项,则可以使用以下正则表达式:

\$[^$]*\$

Note : this will works for all matches if you use modifier g to make it global. 注意 :如果您使用修饰符g使其全局,则此方法适用于所有匹配项。

see the Demo 观看演示

and following for all matches : 和以下所有匹配项:

\$.*?\$

And if you don't want $ you can use capture grouping and return the group after match : 如果您不希望$ ,则可以使用捕获分组并在匹配后返回该组:

\$([^$]*)\$

I need "fafaga", between the two "$" symbols 我需要在两个“ $”符号之间使用“ fafaga”

You can use lookahead and lookbehind for extracting exactly the content between $ 's 您可以使用lookahead和lookbehind来精确提取$之间的内容

(?<=\$)([^$]*)(?=\$)

See DEMO 演示

Note: lookbehinds are not supported by all regex engines 注意:并非所有的正则表达式引擎都支持lookbehinds

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

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