简体   繁体   English

如何在此正则表达式中仅排除数字?

[英]How do I execlude only numbers in this regular expression?

I'm trying to match words that look like this我正在尝试匹配看起来像这样的单词

:test:
:soda_stream:
:test3:

and so on.等等。 What I have now is \:\S+\: and it works for everything except I also have parts in the text that have only numbers, like :23: .我现在拥有的是\:\S+\:它适用于一切,除了我在文本中也有部分只有数字,比如:23: These I want to exclude, but are unsuccessful in doing that.这些我想排除,但没有成功。 I took a look at this StackOverflow page, but it does not work.我看了一下这个StackOverflow 页面,但它不起作用。

Do anyone know how to do it?有谁知道该怎么做? It probably is just some addition that exclude if there is only numbers inside the : 's.如果:中只有数字,它可能只是一些排除。

Taking into account current examples, you may use考虑到当前示例,您可以使用

:(?!\d+:)[^\s:]+:

See the regex demo查看正则表达式演示

Details细节

  • : - a colon : - 一个冒号
  • (?:\d+:) - a negative lookahead that fails the match if there are one or more digits and then : immediately to the right of the current location (?:\d+:) - 如果有一个或多个数字,则匹配失败的负前瞻,然后:紧挨当前位置的右侧
  • [^\s:]+ - 1 or more characters other than whitespace and a colon [^\s:]+ - 除空格和冒号之外的 1 个或多个字符
  • : - a colon : - 一个冒号

As an alternative, you could match at least a single char that is not a digit or a whitespace char in between the colons:作为替代方案,您可以在冒号之间匹配至少一个不是数字或空格字符的单个字符:

:[^:\s]*[^:\s\d][^:\s]*:
  • : Match literally :从字面上匹配
  • [^:\s]* Match 0+ times any char except: or a whitespace char [^:\s]*匹配任何字符 0+ 次,除了: 或空白字符
  • [^:\s\d] Match any char except: or a whitespace char or a digit [^:\s\d]匹配除: 或空白字符或数字以外的任何字符
  • [^:\s]* Match 0+ times any char except: or a whitespace char [^:\s]*匹配任何字符 0+ 次,除了: 或空白字符
  • : Match literally :从字面上匹配

Regex demo正则表达式演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用正则表达式仅识别熊猫数据框中的连续4-5位数字 - How do i use regular expressions to ONLY identify consecutive 4-5 digit numbers within a pandas dataframe 用于电话号码的Python正则表达式 - Python regular expression for phone numbers 带数字的Python正则表达式问题 - Python Regular Expression Problem with Numbers 如何使 python 中的正则表达式仅匹配字母数字而不匹配数字或字母字符? - How can I make regular expression in python match only alphanumeric and not numerical or alphabet characters? 我如何只在python脚本中打印整数 - How do I only print whole numbers in python script 如何使用正则表达式从数据框中分离数字? - How to separate numbers from a data frame using regular expression? 如何使用正则表达式获取链接的 ID“JRLbr6THaTI”? - How do I get "JRLbr6THaTI", the id of the link using regular expression? 如何在python中使用正则表达式在特定单词之前获取特定模式的所有日期或关键字? - How do I get all the dates or keywords of particular patterns before specific word using regular expression in python? 如何在Python 3中使用正则表达式搜索和替换文本中的模式? - How do I search and replace a pattern in text using regular expression in Python 3? 如何使用正则表达式匹配 Python 中的所有 unicode 小写字符? - How do I match all unicode lowercase characters in Python with a regular expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM