Search code examples
javaregexfinite-automataautomatadfa

Matching a string with a regex in Java


I have a DFA but I don't know whether it's accepting states. I only know the regular expression that it accepts. I'm trying to find whether it's accepting states, so I looked into each state of the DFA and I want to compare the accepting regex with the word generated by the current state.

So I'm looking for something that would compare the word with the regex and tell me if it matches, so I could mark this state of the DFA as an accepted state and move on to another state. I was trying to implement some algorithms but it has proven to be quite a complex problem for me. Can you advise me on this? Thanks!

Alphabet: {a,b,c}

Example regular expression: ab.(a|c)*


Solution

  • Take a look at this page: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html

    It's seems like what you are look for is:

    boolean isMatch = Pattern.matches("ab.(a|c)*", str);