[英]Use regex in c# to remove specific combination of characters
I would like to keep in my string the following characters: 我想在字符串中保留以下字符:
At this point, I am able to keep all the alphanumeric characters. 至此,我可以保留所有字母数字字符。 The problem is with the apostroph character, I am keeping all the apostroph whereas I would like to keep only the ones surrounded by alphanumeric characters.
问题在于撇号字符,我保留所有撇号,而我只保留那些由字母数字字符包围的字符。 This is my code :
这是我的代码:
Regex rgx = new Regex("[^a-zA-Z0-9' -]");
string newString = rgx.Replace(oldString, "");
Example : For this string "abc'd1*%'"
, I would like to get "abc'd1"
. 示例:对于此字符串
"abc'd1*%'"
,我想获取"abc'd1"
。
You could use the below regex and then replace the matched characters with an empty string. 您可以使用下面的正则表达式,然后将匹配的字符替换为空字符串。
@"(?<![A-Za-z])'|'(?![A-Za-z])|[^A-Za-z0-9']"
Explanation: 说明:
(?<![A-Za-z])'
Matches all the single quotes which is not preceded by an alphabet. (?<![A-Za-z])'
匹配所有不带字母的单引号。 |
OR '(?![A-Za-z])
Matches all the single quotes which is not followed by an alphabet. '(?![A-Za-z])
匹配所有不带字母的单引号。 So theses two patterns fails to match the single quotes which is preceded and followed by a alphabet. |
OR [^A-Za-z0-9']
From the remaining string, this pattern would match any character but not of alphanumeric or single quotes. [^A-Za-z0-9']
从其余字符串开始,此模式将匹配任何字符,但不匹配字母数字或单引号。 Code: 码:
string str = "abc'd1*%'";
string result = Regex.Replace(str, @"(?<![A-Za-z])'|'(?![A-Za-z])|[^A-Za-z0-9']", "");
Console.WriteLine(result);
Console.ReadLine();
[a-zA-Z0-9 -]+|(?<=[a-zA-Z])'(?=[a-zA-Z])
Try this.See demo. 试试看。看演示。
https://regex101.com/r/dU7oN5/13 https://regex101.com/r/dU7oN5/13
If you are matching whitespace, try this: 如果要匹配空格,请尝试以下操作:
[\w\s-]+|(?<=[\w\s])'(?=[\w\s])
If no whitespace, try this: 如果没有空格,请尝试以下操作:
[\w-]+|(?<=[\w])'(?=[\w])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.