1

I'm having a simple Java Regex issue. I'm trying to get to identigy any pattern that matches the character 'p' + any number, meaning, p1, p10, p100 and so on.

By checking the actual regex at http://regexr.com/, the actuall expression i want is /(p\d+)/

While I have no issues using regex with JavaScript, I'm having a world of trouble with Java.

This is the part of the code that I'm actually trying to get to work properly:

boolean inBounds = (arrayPair.length  == 2);
String c = ("\(p\d+)\");
Pattern p = Pattern.compile(c);
Matcher m = p.matcher(arrayPair[0]);
boolean b = m.matches();

This particular string gives me a invalid escape character, according to this (Invalid escape sequence) I should use double slashes, meaning the line should change to

String c = ("\\(p\\d+)\\");

This gives me a Unmatched closing ')' near index 5 \(p\d+)\" error.

So, I went back to http://regexr.com/ and realized I could write the expression as /p\d+/

So I went back to Java and tried

String c = ("\\p\\d+\\");

This gives a Unknown character property name {\} near index 2 \p\d+\

And it points to the '\' of the 'p\d' part.

Sooooo in another stackoverflow answer somebody mentioned I should use \\ instad of \ for d.

String c = ("\\p\\\\d+\\");

Which lead me to the error Unknown character property name {} near index 2 \p\d+\

Am I missing something here? I'm starting to go crazy about RegEx implementations...

Community
  • 1
  • 1
Johnny Bigoode
  • 578
  • 10
  • 31
  • 2
    Why would you translated the `/` (which is the JS separator for a regex literal) to \ in Java (which is a string/regex escape character) and expect it to work? (In JS, `/(p\d+)/` is the short-hand for `new RegExp("(p\\d+)")`. Notice you have to escape the backslash in the string argument but not the regex literal.) – billc.cn Dec 21 '15 at 13:42

2 Answers2

3

Just use the following Pattern:

"p\\d+"

You don't need to double-escape a literal, only the digit class.

The starting and ending slashes are not required in Java regex, in fact they'd change your pattern.

The parenthesis are used to delimit groups for back-references, which again, seems useless in the case you're illustrating.

Example

String[] test = {"p0", "blap10foo", "p*&^", "d10"};
Pattern p = Pattern.compile("p\\d+");
for (String s: test) {
    Matcher m = p.matcher(s);
    if (m.find()) {
        System.out.printf("Found: %s%n", m.group());
    }
}

Output

Found: p0
Found: p10
Mena
  • 47,782
  • 11
  • 87
  • 106
  • That works fine, thanks! Just wondering... what if I want a /p\d+/g expression? How can I get a global search? – Johnny Bigoode Dec 21 '15 at 14:04
  • 1
    It's a bit different in terms of functionality. To perform the search globally you can use a `while` loop instead of `if`, which will iterate as long as the `matcher.find()` methods returns `true`. In case you are replacing elements, using `replaceAll` will generate a new `String` with all elements replaced. – Mena Dec 21 '15 at 14:07
  • See for multiline: http://stackoverflow.com/questions/3651725/match-multiline-text-using-regular-expression – Leo Izen Dec 21 '15 at 14:08
1

You don't want to backslash the parentheses. Even though SED requires this, in Java the pattern should be String pattern = "(p\\d+)";, which will resolve to the string (p\d+). In this case, the parentheses are actually unnecessary so "p\\d+" would suffice.

Leo Izen
  • 4,165
  • 7
  • 37
  • 56