简体   繁体   中英

Regex - stack trace - match all numbers except java class line number

I'm stuck trying to come up with a regex to match a pattern in java stack trace. This regex should identify all numbers in a string except the line number corresponding to the java class.

For example

str = "(SomeName.java:470) This is the 1st string out of a total of 50 string:345"

I want to write a regex which identifies 1, 50 and 345 and not 470.

I came up with one but it quite doesn't does it for me - "(?<!.java:)[\\d]*" .

This gets 70 instead of 470 and the reason is self explanatory.

Can you please help me with modifying the above regex to match the pattern from the example?

\d+(?![^(]*\))

Try this.See demo.

http://regex101.com/r/oE6jJ1/9

If you have data like 34) the use

^\([^)]*\)|(\d+)

and grab the captures or groups.See demo.

http://regex101.com/r/oE6jJ1/11

You can modify your lookbehind assertion as follows.

(?<!java:)\b\d+

Live Demo

Java code:

String str1 = "(SomeName.java:470) This is the 1st string out of a total of 50 string:345";
String regex1 = "(?<!java:)\\b\\d+";
Matcher m = Pattern.compile(regex1).matcher(str1);
StringBuilder sb = new StringBuilder();
while (m.find()) {
    sb.append(" "+str1.substring(m.start(), m.end()));
}
System.out.println(sb.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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