Ogofo
Ogofo

Reputation: 366

Any suggestion why my regex does not work?

I got the following string to extract some information from:

String: String: String Number;

Right now I'm using the following regex to get the arguments:

(.*?):(.*?):(.*?);$

This way I would get with a Matcher the following output:

group(1) = String
group(2) = String
group(3) = String Number

If I want the number I need to execute another regex on the output of the 3rd group like the following:

([a-zA-Z]* ?([0-9])?$)

Used ont the String String Number this would give me and output like

group(1) = String
group(2) = Number

I thought about combining both steps and use a regex like (.*?):(.*?):([a-zA-Z]* ?([0-9])?);$ on the String: String: String Number;-String. But this does not work and I dont see the reason.

Upvotes: 3

Views: 71

Answers (1)

CaffGeek
CaffGeek

Reputation: 22054

Hwere you go, I added some extra whitespace matching, but this seems to work, you were missing the whitespace between the second : and the following string

 ^(.*?):\s*(.*?):\s*([a-zA-Z]*\s+([0-9])?);$

Upvotes: 3

Related Questions