Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preg_match PHP to java translation

I am having some trouble converting a php pregmatch to java. I thought I had it all correct but it doesn't seem to be working. Here is the code:

Original PHP:

/* Pattern for 44 Character UUID */
$pattern = "([0-9A-F\-]{44})";
if (preg_match($pattern,$content)){
                /*DO ACTION*/
            }

My Java code:

final String pattern = "([0-9A-F\\-]{44})";
    public static boolean pregMatch(String pattern, String content) {
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(content);
            boolean b = m.matches();
            return b;
        }
if (pregMatch(pattern, line)) {
                        //DO ACTION
                    }

So my test input is: DBA40365-7346-4DB4-A2CF-52ECA8C64091-0

Using a series of System.outs I get that b = false.

like image 698
Evilsithgirl Avatar asked Dec 08 '22 20:12

Evilsithgirl


1 Answers

To implement a function as you did in your code:

final String pattern = "[0-9A-F\\-]{44}";
public static boolean pregMatch(String pattern, String content) {
    return content.matches(pattern);
}

And then you can call it as:

if (pregMatch(pattern, line)) {
    //DO ACTION
}

You don't need the parenthesis in your pattern because that just creates a match group, which you are not using. If you need access to back references, you would need the parenthesis an a more advanced regex code using Pattern and Matcher classes.

like image 139
doublesharp Avatar answered Dec 27 '22 21:12

doublesharp