简体   繁体   中英

c# regex formats

I need a regex format:

  1. Only letters

  2. With no html tags

  3. With no these spesific charaters ( and )

Please help.

You can combine your conditions into 1 Regex.Replace since you just remove every match:

SearchBy = Regex.Replace(SearchBy, @"<[^>]+>|[0-9()-]", string.Empty);

Or, since you just want only letters to remain in the string, you can use

SearchBy = Regex.Replace(SearchBy, @"<[^>]+>|\P{L}", string.Empty);

The regex contains 2 alternatives joined with |alternation operator:

  • <[^>]+> - A substring starting with < , then 1 or more characters other than > and then > (however, this might remove anything that looks like a tag but is not a tag!)
  • [0-9()-] - A character class matching all digits from 0 to 9, and literal ( , ) and - (when - is at the end of a character class, we do not have to escape it.)

Or \\P{L} that matches any non-letter.

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