3

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]))

1 Answer 1

5

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]))
Sign up to request clarification or add additional context in comments.

3 Comments

that is almost it, just I don't want to match the digits after the *, meaning I only want numbers at the start of the line. thanks
@London Of course, I imagine, what you want could also be done by splitting your string by "*" and " " (space) and then getting the first element of the array... but I'm by no means a java guru, so I'll leave that to you... :-)
@London LOL (to be honest I hate Java...). You're welcome - glad to have helped! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.