[英]I need to remove all . that don't match a pattern
I need to remove all dots from text unless they match a pattern [0-9]+.[0-9]+ for example if the following text is my input: 我需要从文本中删除所有点,除非它们与模式[0-9] +。[0-9] +相匹配,例如,如果以下文本是我的输入内容:
abc. def. 123.45 ... 12.
the output should look like this: 输出应如下所示:
abc def 123.45 12
Thanks 谢谢
If the language you are using supports lookarounds you can use this regular expression: 如果您使用的语言支持环视 ,则可以使用以下正则表达式:
(?<![0-9])\.|\.(?![0-9])
This matches dots that either aren't preceded by a digit, or aren't followed by a digit. 这会匹配不以数字开头或不以数字开头的点。
Example for C#: C#示例:
string result = Regex.Replace(input, @"(?<![0-9])\.|\.(?![0-9])", "");
If your regex flavor supports negative lookarounds (which .NET does fabulously) you can use this: 如果您的regex风格支持否定环顾(.NET出色地做到了),则可以使用以下方法:
(?<!\d)\.|\.(?!\d)
This will only match dots that have either a non-digit character before or after them. 这只会匹配在其之前或之后具有非数字字符的点。 Simply replace the result with an empty string.
只需将结果替换为空字符串即可。
If not, then you can do this: 如果没有,那么您可以这样做:
(?|(^|\D)\.|\.($|\D))
And replace with $1
. 并替换为
$1
。 This does the same, but includes that additional character in the match. 这样做是一样的,但是在比赛中包括该其他字符。 The replacement puts that matched character back in place.
替换会将匹配的角色放回原处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.