Elad Benda2
Elad Benda2

Reputation: 15492

how to fetch `{` in java regex?

given this text:

RULE deviceType=IPHONE_DEVICE&ClientVersion>=4.10 { bla1 \ } 
RULE deviceType=ANDROID_DEVICE&ClientVersion>=4.5 { bla2 \ }

I want to use java regex to fetch this text:

bla1 
bla2

I have tried:

    Pattern pattern = Pattern.compile("(.*?)RULE deviceType=ANDROID_DEVICE&ClientVersion>=4.5 \\{(\\w+)\\ \\}(.*)");
    Matcher matcher = pattern.matcher(stringMap);
    if (matcher.find()) {
        androidSection = matcher.group(1);
    }

but the code returned zero matches.

What am i missing?

Upvotes: 1

Views: 48

Answers (1)

anubhava
anubhava

Reputation: 785531

You can change your regex to this:

RULE deviceType=\w+&ClientVersion>=[\d.]+\h*\{\h*(\w+)\h*\\\h*}

RegEx Demo

Using Java use this regex:

final String regex =
       "RULE deviceType=\\w+&ClientVersion>=[\\d.]+\\h*\\{\\h*(\\w+)\\h*\\\\\\h*\\}";

Upvotes: 1

Related Questions