如果不在两位数之间,请删除小数点 [英] Remove decimal point when not between two digits
问题描述
我正在清理搜索字符串,需要删除出现的所有句点,但当它们在两位数之间时保留小数点.
I'm cleaning up a search string and need to remove any periods that appear but keep decimal points when they are between two digits.
例如,如果我有一个字符串
For example if I have a string
599.75,老虎.
599.75, Tigers.
然后我希望它回来
599.75,老虎
599.75, Tigers
我在想像这样的一行:
strNewString = RegEx.Replace(strOrigString,strRegEx,string.Empty);
strNewString = RegEx.Replace(strOrigString, strRegEx, string.Empty);
strRegEx将只匹配.的地方,但是我很难弄清楚如何只匹配.的地方.而不是周围的东西.
Where strRegEx would match just the .'s to remove but I'm having a hard time figuring out how to match only the . and not the things surrounding it as well.
推荐答案
您应该利用在断言中向前看和向后看.它们实际上与您输入的字符不匹配,而只是确定是否可以匹配.
您可以使用否定的超前行为和否定的超前行为来执行相反的操作,这在这里是合适的.在 strRegEx
中使用以下内容将匹配不包含数字的句点:
You should take advantage of the lookahead and lookbehind assertions. They don't actually match characters in your input, but only determine whether a match is possible or not.
You can use negative lookaheads and negative lookbehinds to do the opposite of this, which is what's appropriate here. Using the following for strRegEx
will match periods that are not surrounded by digits:
(?<!\d)\.(?!\d)
这篇关于如果不在两位数之间,请删除小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!