简体   繁体   中英

Regex: Do not match if pattern at the end of the string

I have the following regex, where I want to match any explicit dot followed by one or more:

<b> <i> <u> </b> </i> </u>

I would like this Regex to NOT match this pattern if it occurs at the end of the string.

string = Regex.Replace(string, "\.((<[\/biu]+>)+)", ".$1||")

Ex:

This <b>should match.</b> allright.

This <i><b>shouldn't match.</b></i>
"\.((<[\/biu]+>)+)(?!$)"

Use the negative lookahead assertion with the $ symbol to check for end of line. (Remember, $ matches end of line so you want to not match that.)

你可以使用原子分组

\.(?>(?:<\/?[biu]>)+)(?!$)

强制在最后一个闭合元素之后有更多项目,但要确保它们本身不是元素。

"\.((<[\/biu]+>)+)[^<>]+"

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