简体   繁体   中英

How to match a string of tuples in Java?

I have strings like "(C,D) (E,F) (G,H) (J,K)" and "(C,D) (E,F) (G,H) (J,K)" or "((C,D) (E,F) (G,H) (J,K)" . How to return true if regex matches pattern like in first string (which is a one tuple or series of tuples seperated by one whitespace). I tried something like "(\([AZ],[AZ]\)[ |$])+?" , but it does not capture the final pair of tuple. In case of 2nd and 3rd string it should return false.

Here is the problem of your regex:

(\([A-Z],[A-Z]\)[ |$])+?
                ^^^^^

You thought that meant "space or end of string", didn't you? It actually means "space or | or dollar sign". A lot of special characters lose their special meaning when placed inside a character class.

You should replace it with (?: |$) instead. Also, the +? at the end should be a greedy + :

(\([A-Z],[A-Z]\)(?: |$))+

Personally, I don't really like this "space or end of string" thing. I would prefer repeating the tuple pattern (especially when the repeated pattern is not long):

(?:\([A-Z],[A-Z]\) )*(?:\([A-Z],[A-Z]\))

Needless to say, you should match with matches , not find .

If you want to match a string of parenthesised pairs of comma-separated capital letters, with a single space between each pair, you could use a pattern like this:

^\\([A-Z],[A-Z]\\)( \\([A-Z],[A-Z]\\))*$

That is: letter,comma,letter all in parentheses, following by zero or more occurrences of the similar parenthetic expressions, each preceded by a space.

I guess, you might be able to do that with:

\s*|\(([^()\r\n]+)\) 

If the pattern would not return an empty string, would be false .

RegEx Demo

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegularExpression{

    public static void main(String[] args){

        final String regex = "\\([^()\\r\\n]+\\)|\\s*";
        final String string = "(C,D) (E,F) (G,H) (J,K)\n"
             + "(C,D)     (E,F) (G,H) (J,K)\n"
             + "((C,D) (E,F) (G,H) (J,K)";
        final String subst = "";

        final Pattern pattern = Pattern.compile(regex);
        final Matcher matcher = pattern.matcher(string);
        final String result = matcher.replaceAll(subst);

        System.out.println(result);

    }
}


Output

(

If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com . If you'd like, you can also watch in this link , how it would match against some sample inputs.


Source

Regular expression to match balanced parentheses

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM