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...

1
  • 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.) Commented Dec 21, 2015 at 13:42

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

That works fine, thanks! Just wondering... what if I want a /p\d+/g expression? How can I get a global search?
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.
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.

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.