简体   繁体   English

正则表达式匹配字符串(如果结尾)

[英]Regex match a string if its in the end

The following regular expression 以下正则表达式

 (cm)[\s,]

mathches 数学

cm,
cm followed by space

how do i also make it match cm if cm is the last word in a string? 如果cm是字符串中的最后一个单词,我还如何使其与cm匹配?

USe end of the line anchor. 使用行锚的末端。

(cm)(?:[\s,]|$)

You don't need to have a capturing group. 您不需要一个捕获组。

cm(?:[\s,]|$)

This matches cm, or cm<space> or last cm 这匹配cm,cm<space>或最后一个cm

cm(?=\s|$|,)

试试这个。这应该起作用。

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

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