python - 正则表达式匹配字符串,其中单词后跟空格,然后是数字点或连字符,单词后跟空格,然后(一些信息)
问题描述
我有一个具有以下格式的字符串:
name category (more info)
例如:Foo Bar-8.io 5.61.0-rc-1 (data)
我需要一个正则表达式,它基本上过滤掉符合上述格式的字符串。
名称可以是带有空格的字母数字,-
并且.
类别可以以数字开头,后跟单词,包括点或连字符
数据可以是任何.*
包含在()
我试过这个:^[\w\s]+.*\s.*\(.*\)$
但似乎没有涵盖上述模式。
解决方案
利用
^(.*)\s+(\S+)\s+\((.*)\)$
请参阅正则表达式证明。
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
\S+ non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
\) ')'
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
推荐阅读
- javascript - 从其他可观察对象切换 Rxjs 流
- java - Java 代码(按预期)适用于除一个输入之外的众多输入:500,000
- javascript - 动态图像数量时在html中水平分布
- package - Tidyverse 和 Magrittr 函数中的错误
- reactjs - 开玩笑模拟 window.scrollTo
- java - 如何使用 AWS Java SDK 以 s3api 查询获取对象列表?
- sql-server - 连接来自子查询的字符串
- javascript - 编译代码后如何使用nodemon?
- python - 如何解释 xgboost 树中的叶索引
- ruby - 如何使用 Ruby/Tk 验证 TkEntry 的内容?