Rene
Rene

Reputation: 5

Matching String regex with Optional (foobar)

I have this data:

/blabla/blabla (abs,def)

/yxz

I use this regex

(.*)(?:\(([^$]*)\))?\n

But it doesn't work, and i don't know whats wrong.

I need the first "directory" information and optional the Information in "()".

Upvotes: 0

Views: 59

Answers (3)

buckley
buckley

Reputation: 14079

This regex extracts the first directory in group 1 and anything between the () optionally:

/([^/]*)(?:\((.*?)\)|.)*

Let me know if this works or need some assistance.

Match 1:    /blabla/blabla (abs,def)         0      24
Group 1:    blabla       1       6
Group 2:    abs,def     16       7
Match 2:    /yxz        28       4
Group 1:    yxz     29       3
Group 2 did not participate in the match

edit for quick joe

Upvotes: 1

dhchen
dhchen

Reputation: 1198

Try using some online regexp matcher (ex: http://www.rubular.com/ ) to test by your own. Many of them has the match highlight function, and you can refine your regex by them

Upvotes: 1

user1096188
user1096188

Reputation: 1839

something like this, maybe? ([^(\n]+)(?:\(([^)]*)\))?

Upvotes: 0

Related Questions