简体   繁体   English

我需要删除所有内容。 与模式不符

[英]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])", "");

See it working online: ideone 看到它在线上工作: ideone

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.

相关问题 如何删除所有与模式不匹配的符号 - How to delete all symbols that don't match the pattern 访客模式的变体-默认的无操作访客方法,因此添加元素类型时不需要更改所有访客吗? - Variant on Visitor pattern - default no-op Visit method so I don't need to change all my visitors when I add an element type? 当 arguments 与给定模式不匹配时,如何让 NSubstitute 模拟失败? - How do I get NSubstitute mocks to fail when the arguments don't match the given pattern? 如何投影以从与过滤器不匹配的数组中删除属性 - How do I project to remove attributes from an array that don't match a filter 需要正则表达式匹配模式 - Need Regex match pattern 需要RegEx模式匹配&#39; - &#39; - Need RegEx Pattern To Match '--' 如果不需要所有属性,是否可以将数据从一个对象放到另一个对象? - Can I put the data from one object to another if I don't need all of the properties? 如果我真的不需要它们,是否应该获取所有数据库表字段? - Should I get all database table fields if I don't really need them? 如果我不需要所有功能,如何实现接口? - How do I implement an interface if I don't need all of its functions? 为什么我不创作所有作品? - Why I don't create all the pieces?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM