简体   繁体   中英

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?

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(?=\s|$|,)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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