2

I am writing a sample application in which I've came across the following problem:

I have to check to characters in terms of their "compatibility".

boolean checkIfPairCorrect(Character c1, Character c2) {...}

In a nutshell it is sth like: "Check if these characters make correct open-close pair". For example:

Pair "(" and ")" returns TRUE
Pair "{" and "}" returns TRUE
Pair "(" and "}" returns FALSE // incompatibile brackets
Pair ")" and "(" returns FALSE // wrong order
Pair "a" and "3" returns FALSE // 'a' or '3' are not the characters for which "closing" char can be found

You would say: "Simply write a regexp man!!!" using some Internet help like:

Regular Expression to match outer brackets
Regex to restrict only one occurrence of open and close brackets using C#
Regexp and brackets article

And you would be right, but... The problem is difficult for me because function can take some exotic characters as parameters (UTF-8 encoding) like japanese, turkish, arabic etc. I do not know what characters can be treated as open-close pair in other languages(cultures), so I am looking for a library which can deal with my problem.

Question: Do you know any library which could be helpful in my case, or do you have any tips how to deal with it?

Community
  • 1
  • 1
guitar_freak
  • 4,325
  • 6
  • 27
  • 43
  • 2
    I'd think that after [this answer](http://stackoverflow.com/a/546457/335858) it would be clear that you cannot do it in regexp, even theoretically: the regexp model does not support counting. That's a wrong tool for the job. – Sergey Kalinichenko Apr 28 '13 at 14:43
  • @dasblinkenlight this is not about parsing longer text, this is about finding pair for a character. And you can certainly do that with a regexp. – hyde Apr 28 '13 at 14:48
  • @hyde Yes, you are right. – guitar_freak Apr 28 '13 at 14:51
  • 2
    @dasblinkenlight Unicode has character classes `Ps` (opening bracket characters) and `Pe` (closing bracket characters). But I don't know any way to check if they match. – Alexey Apr 28 '13 at 14:51
  • 1
    @dasblinkenlight Ok... my digresion about regex was not a good idea (in my app I will use it, but it is not relevant as far as this question is concerned) – guitar_freak Apr 28 '13 at 14:52
  • @Alexey Thanks for this tip – guitar_freak Apr 28 '13 at 14:53

2 Answers2

3

Why not have a map like this:

Map<Character, Character> pairs = new HashMap<Character, Character>();
pairs.put('(', ')');
pairs.put('{', '}');
pairs.put('[', ']');
...

Then your method could be

boolean checkIfPairCorrect(char c1, char c2) {
    return pairs.get(c1) == c2;
}
arshajii
  • 118,519
  • 22
  • 219
  • 270
  • was about to recommend same, you were too fast! :-) – sanbhat Apr 28 '13 at 14:45
  • 2
    Yeah yeah... That's of course right, but the question is if you know any library which has all pairs ready to use... I mean which "konws" for instance all possible open-close pairs UTF-8 encoding ;) – guitar_freak Apr 28 '13 at 14:49
1

For completeness, here's how you can do this with regexp:

boolean checkIfPairCorrect(Character c1, Character c2) {
    String str = c1.toString() + c2.toString();
    return str.matches(
          "(\\[\\])"
       + "|(\\(\\))"
       + "|({})"
       // ... add any more you may want 
       );
}

But I would do it using a map, as shown by A.R.S's answer.

About your actual question, I don't know if there's a library, and I don't think eg. unicode has some neat way to determine thise pairs. So your best bet might be to use the map method, and hard-code the pairs yourself, perhaps from the list in this Wikipedia page.

hyde
  • 50,653
  • 19
  • 110
  • 158