user1419243
user1419243

Reputation: 1705

How to use plus character in regex

I have written a snippet, but it doesn't work correctly.

I have an input in this format:

Arg2+res=(s11_19,s11_20,s11_21,s11_22),Arg4-res=()

It can contain multiple Args (e.g. Arg1, Arg2, ...). What I want, is to return +resinstances. For example, in the above example, I need this part:

Arg2+res=(s11_19,s11_20,s11_21,s11_22)

My Regex is like the following:

 Pattern p = Pattern.compile("Arg\\d+\\+res=\\(\\S+\\)");
    Matcher m = p.matcher(ove_imp_roles);
    while (m.find()) {

        System.out.println(m.group());
    }

The code has two problems:

1) It returns the whole string as a single match. For example, in the above sentence it returns Arg2+res=(s11_19,s11_20,s11_21,s11_22),Arg4-res=() as the matching instance.

Even if both instances include Arg1+res, it returns the whole string as a single match, while I expect it to be returned as two different matches.

2) The code counts instances with -res, too, while I don't need them.

Can anyone help me with this problem?

Update: I checked the code again and updated the above question correspondingly. The problem with -res occurs when it includes empty brackets (for example Arg1-res=().

Thanks in advance,

Upvotes: 0

Views: 363

Answers (2)

Alexis C.
Alexis C.

Reputation: 93892

The problem is (\\S+\\). If you have the following input:

String s = "Arg2+res=(s1355_19,s1355_20);Arg3-res=(s1355_19,s1355_20)";

Arg\\d+\\+res=\\( matches Arg2+res=( and then S+ will match (because the + is greedy):

s1355_19,s1355_20);Arg3-res=(s1355_19,s1355_20

So you can make it lazy, so that it stops as soon as it finds the first right parenthesis in the input:

Pattern p = Pattern.compile("Arg\\d+\\+res=\\(\\S+?\\)");

Alternatively, you can split the input by ';' and see if each String matches "^Arg\\d+\\+.*$"

Upvotes: 1

anubhava
anubhava

Reputation: 786081

You're calling m.find() inside while(m.find()), make it like this:

 Pattern p = Pattern.compile("Arg\\d+\\+res=\\(\\S+\\)");
 Matcher m = p.matcher(ove_imp_roles);
 while (m.find()) {
     System.out.println(m.group());
 }

btw your regex is matching 2nd Arg correctly


Based on the edited question and new input OP can use this regex:

Pattern p = Pattern.compile("Arg\\d+\\+res=\\([^)]+\\)");

[^)]+ will match 1 or more characters that are not ).

Upvotes: 3

Related Questions