仅从字符串的开头和结尾删除非字母数字字符

时间:2022-09-15 16:09:34

I am trying to clean up some data using a helper exe (C#).

我试图使用帮助程序exe(C#)清理一些数据。

I iterate through each string and I want to remove invalid characters from the start and end of the string i.e. remove the dollar symbols from $$$helloworld$$$.

我遍历每个字符串,我想从字符串的开头和结尾删除无效字符,即从$$$ helloworld $$$中删除美元符号。

This works fine using this regular expression: \W.

使用这个正则表达式可以正常工作:\ W.

However, strings which contain invalid character in the middle should be left alone i.e. hello$$$$world is fine and my regular expression should not match this particular string.

但是,中间包含无效字符的字符串应该保持不变,即hello $$$$ world是正常的,我的正则表达式不应该与此特定字符串匹配。

So in essence, I am trying to figure out the syntax to match invalid characters at the start and the end of of a string, but leave the strings which contain invalid characters in their body.

所以从本质上讲,我试图找出在字符串的开头和结尾匹配无效字符的语法,但是留下包含无效字符的字符串。

Thanks for your help!

谢谢你的帮助!

5 个解决方案

#1


6  

This does it!

这样做了!

(^[\W_]*)|([\W_]*$)

This regex says match zero or more non word characters at the start(^) or(|) at the end($)

这个正则表达式表示在开头(^)或(|)结束时匹配零个或多个非单词字符($)

#2


0  

The following should work:

以下应该有效:

^\W+|\W+$

^ and $ are anchors to the beginning and end of the string respectively. The | in the middle is an OR, so this regex means "either match one or more non-word characters at the start of the string, or match one or more non-word characters at the end of the string".

^和$分别是字符串开头和结尾的锚点。 |中间是一个OR,所以这个正则表达式意味着“要么匹配字符串开头的一个或多个非单词字符,要么匹配字符串末尾的一个或多个非单词字符”。

#3


0  

Use ^ to match the start of string, and $ to match the end of string. C# Regex Cheat Sheet

使用^匹配字符串的开头,使用$匹配字符串的结尾。 C#Regex备忘单

#4


0  

Try this one,

试试这个,

 (^[^\w]*)|([^\w]*$)

#5


0  

Use ^ to match 'beginning of line' and $ to match 'end of line', i.e. you code should match and remove ^\W* and \W*$

使用^来匹配'line of line'和$匹配'end of line',即你的代码应匹配并删除^ \ W *和\ W * $

#1


6  

This does it!

这样做了!

(^[\W_]*)|([\W_]*$)

This regex says match zero or more non word characters at the start(^) or(|) at the end($)

这个正则表达式表示在开头(^)或(|)结束时匹配零个或多个非单词字符($)

#2


0  

The following should work:

以下应该有效:

^\W+|\W+$

^ and $ are anchors to the beginning and end of the string respectively. The | in the middle is an OR, so this regex means "either match one or more non-word characters at the start of the string, or match one or more non-word characters at the end of the string".

^和$分别是字符串开头和结尾的锚点。 |中间是一个OR,所以这个正则表达式意味着“要么匹配字符串开头的一个或多个非单词字符,要么匹配字符串末尾的一个或多个非单词字符”。

#3


0  

Use ^ to match the start of string, and $ to match the end of string. C# Regex Cheat Sheet

使用^匹配字符串的开头,使用$匹配字符串的结尾。 C#Regex备忘单

#4


0  

Try this one,

试试这个,

 (^[^\w]*)|([^\w]*$)

#5


0  

Use ^ to match 'beginning of line' and $ to match 'end of line', i.e. you code should match and remove ^\W* and \W*$

使用^来匹配'line of line'和$匹配'end of line',即你的代码应匹配并删除^ \ W *和\ W * $