简体   繁体   English

匹配Java正则表达式中的字符

[英]Matching characters in Java regex

I'd like to extract some digits that are not followed by star, here is the example : 我想提取一些未被星号跟随的数字,这是示例:

1
0
0*1543
27 123*5464
11 1007*
998*7586
13
997*686

I've tried using this regex and some more but none of them worked: 我已经尝试过使用这个正则表达式而且还有一些但是没有一个工作:

(\d+)[^\*]

What I'm looking for is to get all digits unless they're followed by a star. 我正在寻找的是获得所有数字,除非他们后面跟着一个明星。 Here is how the output should look like : 以下是输出的外观:

1
0
27
11
13

My plan was to write regex and use groups to extract these values. 我的计划是编写正则表达式并使用组来提取这些值。 I'm using this site to test my regex. 我正在使用这个网站来测试我的正则表达式。

Update : I'm not looking for match the digits after the *, meaning I only want numbers at the start of the line. 更新:我不是在找*之后的数字匹配,这意味着我只想在行的开头找到数字。 thanks 谢谢

Update II: 更新II:

Aftert further diging into matches given by regex(from Dr.Kameleon) 进一步挖掘正则表达式给出的匹配(来自Dr.Kameleon)

(?<!\*)(?<![0-9])([0-9]+(?!\*)(?![0-9])) 

I get an extra match as it can be seen here : 我可以在这里看到额外的匹配:

http://regexr.com?30l69 http://regexr.com?30l69

I don't want to match 505 in 222*123 505 . 我不想匹配505222*123 505 All other matches are perfectly matched. 所有其他比赛完全匹配。

Looks like this regex need small tweaking but I wasn't able to tweak it ie (?<!\\*)(?<![0-9])(?<!\\\\s)([0-9]+(?!\\*)(?![0-9])) 看起来这个正则表达式需要小调整,但我无法调整它,即(?<!\\*)(?<![0-9])(?<!\\\\s)([0-9]+(?!\\*)(?![0-9]))

Try this one : (demo : http://regexr.com?30l47 ) 试试这个:(演示: http //regexr.com? 30l47

([0-9]+(?!\*)(?![0-9]))

or this (if you want JUST the first parts, as in your example) - http://regexr.com?30l4a : 或者这个 (如果你只想要第一部分,如你的例子) - http://regexr.com?30l4a

(?<!\*)(?<![0-9])([0-9]+(?!\*)(?![0-9]))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM