我有一个用于识别牙齿的正则表达式,正则表达式如下:
/((U|L)(R|L)[0-8a-eA-E](\s|\/|-|_)?((M|O|D|I|B|P|L)+)?)/gi它应该(并且确实)匹配以下内容:
UR7
UR7 MOD
UR5-MOD
问题是,最后一部分(在UR和数字之后)是可选的,因此可能是它们没有输入,这很好但是遗憾的是可选空间仍然匹配,例如:
UR7(请注意UR7之后的空间)以及空间。
我可以看到两种方法来解决这个问题:
如果下一组匹配,只匹配该组(\ s | / | - | _)? 要么 如果它是匹配的最后一个字符,则不匹配组(\ s | / | - | _)。谁能帮助我实现这个目标?
所以上述匹配所需的行为是匹配的,但对于带有空格的UR7,只匹配UR7而不是空格!
非常感谢!
I have a Regex that is used to recognise tooth, The regex is as follow:
/((U|L)(R|L)[0-8a-eA-E](\s|\/|-|_)?((M|O|D|I|B|P|L)+)?)/giIt should (and does) match the following:
UR7
UR7 MOD
UR5-MOD
The problem is, The last part (after the UR and the number) are optional so it could be that they are not entered, which is fine however unfortunately the optional space is still being matched so for example:
UR7(Please notice the space after the UR7) matches the space as well ..
I can see two ways to tackle this:
Only match this group (\s|/|-|_) if the next group matches? or Do not match the group (\s|/|-|_) if it is the last character of a match.Could anyone please help me to achieve this?
So the desired behaviour is for all the above matches to match but for a UR7 with a space after to match only the UR7 and not the space!
Thank you so much!
最满意答案
仅当分隔符后跟更多内容时,才将分隔符标记为可以容忍。
我还重构了为单个字符使用字符类,因为这通常更有效。
/[UL][RL][0-8a-eA-E]((\s|[-\/_])?[MODIBPL]+)?/giMark the separator as tolerated only when it is followed by something more.
I have also refactored to use character classes for single characters, as that is typically more efficient.
/[UL][RL][0-8a-eA-E]((\s|[-\/_])?[MODIBPL]+)?/gi更多推荐
发布评论