简体   繁体   English

c#正则表达式格式

[英]c# regex formats

I need a regex format:我需要一个正则表达式格式:

  1. Only letters只有字母

  2. With no html tags没有html标签

  3. With no these spesific charaters ( and )没有这些特定的字符()

Please help.请帮忙。

You can combine your conditions into 1 Regex.Replace since you just remove every match:您可以将您的条件组合到 1 Regex.Replace因为您只需删除每个匹配项:

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 |正则表达式包含 2 个与|连接的替代方案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!) <[^>]+> - 以<开头的子字符串,然后是 1 个或多个除>>之外的字符(但是,这可能会删除任何看起来像标签但不是标签的东西!)
  • [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.) [0-9()-] - 匹配从 0 到 9 的所有数字的字符类,以及文字( , )- (当-位于字符类的末尾时,我们不必对其进行转义。)

Or \\P{L} that matches any non-letter.或匹配任何非字母的\\P{L}

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

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