简体   繁体   中英

Java Regex issues with single character followed by number

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM