我有一个包含以下几行的文本文件:
# this configuration is for gmail
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_HOST_PASSWORD = 'test'
EMAIL_PORT = 587
我想删除数字中的所有内容,包括“-”,因此结果如下:
asm-java-2.0.0-lib
cib-slides-3.1.0
lib-hibernate-common-4.0.0-beta
astp
act4lib-4.0.0
有人知道正确的正则表达式吗?到目前为止,我想出了2.0.0-lib
3.1.0
4.0.0-beta
act4lib
,但是它有太多错误。
答案 0 :(得分:2)
^.*?(?=\d|$)
LEAVE EMPTY
. matches newline
说明:
^ # beginning of line
.*? # 0 or more any character but newline, not greedy
(?= # start lookahead, zero-length assertion that makes sure we have after
\d # a digit
| # OR
$ # end of line
) # end lookahead
给定示例的结果
2.0.0-lib
3.1.0
4.0.0-beta
处理act4lib-4.0.0
的另一种解决方案:
^(?:.*-(?=\d)|\D+)
LEAVE EMPTY
. matches newline
说明:
^ # beginning of line
(?: # start non capture group
.* # 0 or more any character but newline
- # a dash
(?=\d) # lookahead, zero-length assertion that makes sure we have a digit after
| # OR
\D+ # 1 or more non digit
) # end group
替换:
\t # a tabulation, you may replace with what you want
给出:
asm-java-2.0.0-lib
cib-slides-3.1.0
lib-hibernate-common-4.0.0-beta
astp
act4lib-4.0.0
给定示例的结果
2.0.0-lib
3.1.0
4.0.0-beta
4.0.0
答案 1 :(得分:1)
使用
^\D+\-
如果您要完全删除没有数字的行,请使用此
^\D+(\-|$)
如果软件包的名称中包含数字,例如act4lib-4.0.0
,则需要更长的变体
^[\w-]+(\-(?=\d+\.\d+)|$)
它可以缩短为^.+?(\-(?=\d+\.)|$)
,但我只是想确定一下,所以我还要检查次要版本号
^
从行首开始匹配