如何在lex中选择内部匹配

时间:2021-10-12 09:37:00

am new to lex and I wanna take all the matches specific to a regular expression

我是lex的新手,我想把所有比赛都特定于正则表达式

for example in the following text :

例如,在下面的文字中:

/* text text

text 
text

text */

text text
/* text text text text text text

text text */

i wanna choose the two matches between /* and */

我想选择/ *和* /之间的两场比赛

but lex matches the whole outer match and doen't return the two! I use this expression :

但是lex匹配整个外围比赛,并没有返回两个!我用这个表达式:

\/\*(.|\n)*\*\/

How to select inner matches instead of the whole outer one? thank you

如何选择内部匹配而不是整个外部匹配?谢谢

1 个解决方案

#1


\/\*([^*]|\n|\*+[^*/])*\*+\/

What's going on is that * is greedy -- it will match as long of a string as possible. The preceding expression treats the character * separately by ensuring that the regular expression can continue only as long as it is not followed by the character /. This is accomplished by having the interior units of the regular expression be one of

发生的事情是*贪婪 - 它会尽可能长地匹配。前面的表达式通过确保正则表达式只有在字符/后面没有后跟的情况下才能单独处理字符*。这是通过使正则表达式的内部单元为其中之一来实现的

  • a character that's not *
  • 一个不是*的角色

  • a newline
  • a string of *s followed by a character that's not /
  • 一串* s后跟一个不是/的字符

At the end, there is a string of *s followed by a /. (Note: a previous version did not handle this case correctly. I really wish that flex had the *? operator.)

最后,有一个* s后跟一个/的字符串。 (注意:以前的版本没有正确处理这种情况。我真的希望flex有*?运算符。)

#1


\/\*([^*]|\n|\*+[^*/])*\*+\/

What's going on is that * is greedy -- it will match as long of a string as possible. The preceding expression treats the character * separately by ensuring that the regular expression can continue only as long as it is not followed by the character /. This is accomplished by having the interior units of the regular expression be one of

发生的事情是*贪婪 - 它会尽可能长地匹配。前面的表达式通过确保正则表达式只有在字符/后面没有后跟的情况下才能单独处理字符*。这是通过使正则表达式的内部单元为其中之一来实现的

  • a character that's not *
  • 一个不是*的角色

  • a newline
  • a string of *s followed by a character that's not /
  • 一串* s后跟一个不是/的字符

At the end, there is a string of *s followed by a /. (Note: a previous version did not handle this case correctly. I really wish that flex had the *? operator.)

最后,有一个* s后跟一个/的字符串。 (注意:以前的版本没有正确处理这种情况。我真的希望flex有*?运算符。)