0

I'm doing some transliteration with java and everything works great, but it would be nice to have matched pattern. Is it possible?

For example:

for surname GULEVSKAIA I generate such pattern

(^g+(yu|u|y)l+(io|e|ye|yo|jo|ye)(v|b|w)+(s|c)+(k|c)+a(ya|ia|ja|a|y)(a)*)

can I somehow get information, that actually matched

g

u

l

e

...

etc

As you can see, sometimes it is NOT one letter.

2
  • 4
    When you have things in ( ) in a regex, it defines a "capture group" that you can then query with Matcher methods. See this tutorial: docs.oracle.com/javase/tutorial/essential/regex/groups.html. Commented Sep 30, 2016 at 6:02
  • Thank you. So simple... I have to move all chars (like g or a) to groups, but it is not a big problem. Commented Sep 30, 2016 at 6:33

1 Answer 1

1

You may achieve this , once pattern is matched , retrive the macthed string using group() method of Matcher class passing 0 as value. then convert that string to chars array and print those characters like below

  String line = "gulevskaia";
  String pattern = "(^g+(yu|u|y)l+(io|e|ye|yo|jo|ye)(v|b|w)+(s|c)+(k|c)+a(ya|ia|ja|a|y)(a)*)";


  Pattern r = Pattern.compile(pattern);
  Matcher m = r.matcher(line);

  if (m.find( )) {
     System.out.println("Found value: " + m.group(0) );
     char chars[] =m.group(0).toCharArray();
     for(int i=0;i<chars.length;i++)
         System.out.println(chars[i]);

  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.