1

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 "(\([A-Z],[A-Z]\)[ |$])+?", but it does not capture the final pair of tuple. In case of 2nd and 3rd string it should return false.

  • Why do you use the lazy version of `+` instead of the eager one? If you write `+?` it will match only as much as it has to match to satisfy the expression instead of all of it. – Jannik Nov 17 '19 at 15:40

3 Answers3

2

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.

Sweeper
  • 145,870
  • 17
  • 129
  • 225
1

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.

khelwood
  • 46,621
  • 12
  • 59
  • 83
0

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

Community
  • 1
  • 1
Emma
  • 1
  • 9
  • 28
  • 53