London
London

Reputation: 15284

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:

Aftert further diging into matches given by regex(from Dr.Kameleon)

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

I get an extra match as it can be seen here :

http://regexr.com?30l69

I don't want to match 505 in 222*123 505 . All other matches are perfectly matched.

Looks like this regex need small tweaking but I wasn't able to tweak it i.e. (?<!\*)(?<![0-9])(?<!\\s)([0-9]+(?!\*)(?![0-9]))

Upvotes: 3

Views: 675

Answers (1)

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Try this one : (demo : 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 :

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

Upvotes: 5

Related Questions