正则表达式模式检测像$$ _ $$这样的东西

时间:2021-04-19 20:15:24

I am trying to write a regex that works like below if I have something like

如果我有类似的东西,我正在尝试编写一个如下所示的正则表达式

Hi $$var1$$ $$var2$$ how are you?

it should return me two matches

它应该给我两场比赛

$$var1$$ -1st match

$$var2$$ -2nd match

Currently i have made a regex /\$\$\w+\d*\W*\$\$/gi this works fine if the two patterns are not next to each other for eg:

目前我已经制作了一个正则表达式/ \ $ \ $ \ w + \ d * \ W * \ $ \ $ / gi如果这两个模式不是彼此相邻的,这可以正常工作,例如:

Hi $$var1$$ and $$var2$$ how are you?

it detects two matches, but if these two matches are next to each other like in the first example seperated by space it detects $$var1$$ $$. Can someone help me fix this regex?

它会检测到两个匹配项,但如果这两个匹配项彼此相邻,就像第一个以空格分隔的示例一样,它会检测到$$ var1 $$ $$。有人可以帮我解决这个正则表达式吗?

2 个解决方案

#1


 \$\$\w+\d*?\W*?\$\$
              ^^

Make your \W non greedy as it will eat up $$ $$ but not $$ and $$ as it cannot consume and.See demo.

让你的\ W非贪婪,因为它会消耗$$ $$而不是$$和$$,因为它不能消耗和。参见演示。

https://regex101.com/r/tP7qE7/5

#2


I don't know all your restrictions, but it might be easier and faster with a negated class:

我不知道你所有的限制,但是否定课程可能更容易,更快:

\$\$[^$]+\$\$

If single dollar signs are allowed between the double ones, then you might use something like this to keep up the same speed:

如果在双重符号之间允许单个美元符号,那么您可以使用这样的东西来保持相同的速度:

\$\$(?:[^$]+|\$(?!\$))+\$\$

(?:[^$]+|\$(?!\$))+ matches either a non $ or one $ that is not followed by a second $ (though at that point, \$\$.+?\$\$ would actually be simpler should that be the case).

(?:[^ $] + | \ $(?!\ $))+匹配非$或$ $后面没有第二个$(虽然在那时,\ $ \ $。+?\ $如果是这样的话,\ $实际上会更简单)。

The reason why your regex was not behaving as you expected was that \W* can potentially match any dollar signs. So if there was anything that does not match \w in between the last dollar signs of pattern you want to match, your regex would continue matching until the next double dollar signs.

你的正则表达式没有像你预期的那样表现的原因是\ W *可以匹配任何美元符号。因此,如果在您要匹配的模式的最后一个美元符号之间存在任何与\ w不匹配的内容,则您的正则表达式将继续匹配,直到下一个双美元符号。

#1


 \$\$\w+\d*?\W*?\$\$
              ^^

Make your \W non greedy as it will eat up $$ $$ but not $$ and $$ as it cannot consume and.See demo.

让你的\ W非贪婪,因为它会消耗$$ $$而不是$$和$$,因为它不能消耗和。参见演示。

https://regex101.com/r/tP7qE7/5

#2


I don't know all your restrictions, but it might be easier and faster with a negated class:

我不知道你所有的限制,但是否定课程可能更容易,更快:

\$\$[^$]+\$\$

If single dollar signs are allowed between the double ones, then you might use something like this to keep up the same speed:

如果在双重符号之间允许单个美元符号,那么您可以使用这样的东西来保持相同的速度:

\$\$(?:[^$]+|\$(?!\$))+\$\$

(?:[^$]+|\$(?!\$))+ matches either a non $ or one $ that is not followed by a second $ (though at that point, \$\$.+?\$\$ would actually be simpler should that be the case).

(?:[^ $] + | \ $(?!\ $))+匹配非$或$ $后面没有第二个$(虽然在那时,\ $ \ $。+?\ $如果是这样的话,\ $实际上会更简单)。

The reason why your regex was not behaving as you expected was that \W* can potentially match any dollar signs. So if there was anything that does not match \w in between the last dollar signs of pattern you want to match, your regex would continue matching until the next double dollar signs.

你的正则表达式没有像你预期的那样表现的原因是\ W *可以匹配任何美元符号。因此,如果在您要匹配的模式的最后一个美元符号之间存在任何与\ w不匹配的内容,则您的正则表达式将继续匹配,直到下一个双美元符号。