mcfly soft
mcfly soft

Reputation: 11651

Regular expression which matches a String also containing brackets

I have the following text

bla bla ==   aaaaaa(bb)aaaaaa  == bla bla

I would like to find the String (Match)

==   aaaaaa(bb)aaaaaa   ==

I do not know how many spaces are between the == and the aaaaaa are, so the text could also be

bla bla ==          aaaaaa(bb)aaaaaa  == bla bla

in that case I would like to match

==          aaaaaa(bb)aaaaaa  ==

I tried with following regex, but I realize that the brackets are not recognized:

==(.+?)aaaaaa(bb)aaaaaa(.+?)==

Howto do ?

Upvotes: 0

Views: 66

Answers (2)

user4325551
user4325551

Reputation:

Put a backslash before the ( and ) to use them as regular characters, this must work:

==(.+?)aaaaaa\(bb\)aaaaaa(.+?)==

Upvotes: 2

ajb
ajb

Reputation: 31689

If you want to match ( and ) in your string, you need to put backslash characters before them. I think you probably want something like

Pattern pat = Pattern.compile("==(.+?)aaaaaa\\(bb\\)aaaaaa(.+?)==");

(You need to include the backslash twice in a string literal in order to get one backslash into the actual pattern.)

Upvotes: 1

Related Questions