正则表达式 - 避免字符
在 C# 中使用一些正则表达式时,我面临以下问题:
考虑这个简单的字符串:~0~这是一个简单的文本~POP~NIZ~0~0~
我想选择任何两个“~”之间的字符串,包含超过 3 个字符,当然“~”除外。在我的示例中,将是:
这是一个简单的文本,
我可以制作类似以下内容的内容:([\w]|[\d]|.|\,...... .){4-500}
我会以一个非常长的正则表达式结束,无法调试且不可读...
相反,我更愿意创建一个像 " 给我任何字符,除了 '~' 的正则表达式包含在 '~' 和 '~' 之间”。
我找不到正确使用 [^] 的方法!
我该怎么做?
提前致谢 !
答案:我终于做到了:~[^~]{3,}~
它需要除“~”之外的所有内容,包含在两个“~”之间,并且超过三个字符很长。
感谢您的回答!
While using some regex in C#, I'm facing the following problem :
Consider this simple string : ~0~This is a simple text~POP~NIZ~0~0~
I would like to pick any strings between two '~', that contains more than 3 chars except of course the '~'. In my example, the would be :
This is a simple text
I could make something like : ([\w]|[\d]|.|\,.................){4-500}
I would end with a really long regex, impossible to debug and not readable...
Instead, I would prefer to create a regex like "Give me any characters, except '~' that is contained between '~' and '~' ".
I can't find a way to use [^] properly !
How can I do this ?
Thanks in advance !
ANSWER : I've finally done this : ~[^~]{3,}~
It takes everything but '~', contained between two '~' and that is more than three chars long.
Thanks for your answers !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不介意从开始和结束时可能出现额外的批次,那么它应该很简单:
或者,您可以拆分并获取较长的批次:
如果您确实想将字符限制为特定的集合,则您可以可以使用lookahead和backward来确定。这不会消耗波浪号,因此您会得到
~123~abc~
的两个匹配项(同样,如果您习惯的话,可以使用[^~]
):If you don't mind a possible extra batch from the start and the end, it should be as easy as:
Or, you can just split and take the long ones:
If you do want to limit the characters to a specific set, you can use lookahead and behind to make sure. This will not consume the tilde signs, so you get two matches for
~123~abc~
(again, you can use[^~]
if you are move comfortable with it):试试这个正则表达式
(?:~([^~]{3,})~)
它将匹配两个 ~~ 之间的所有内容(不会捕获 ~)
Try this regex
(?:~([^~]{3,})~)
It will match everithing between two ~~ (wont catch ~)
像这样的东西:(
测试过)
Something like:
(tested)