¿Expresión regular en Java? Estoy atascado

I got some text that changes dynamically and I need a way to find some parts in it. Especially like these:




So my values always starts with an "+" plus symbol then some digits, minimum one and then the "now" word.

I tried many ways like this:

if(myString.contains("+[0-9]+now")) //false

but I tired of it... can you help please?

preguntado el 29 de agosto de 12 a las 14:08

String#contains takes a string as a parameter, not a regex. -

6 Respuestas

Utiliza String#matches() en lugar:

if (myString.matches(".*\\+[0-9]+now.*"))

También, trabaja para + is a special regex character, that's why you need to escape it.

Si necesitas capturar the numbers, use Pattern y Matcher:

Pattern p = Pattern.compile("\\+([0-9]+)now");
Matcher m = p.matcher(myString);
while (m.find()) {

() is a capturing group, meaning it will tell the regex engine to tienda the matched content, so that you can retrieve it later with group().

Respondido 29 ago 12, 14:08

OP asks about a part of the string, not the whole string - Qnan

Prueba esto ......

Pattern pat = Pattern.compile("\\+\\d+now");
Matcher mat = pat.matcher("Input_Text");


   // Do whatever you want to do with the data now...


Respondido 29 ago 12, 14:08

yup..... thats right.... mat.group() or mat.group(0) both will do here.... i just left it to OP - Kumar Vivek Mitra

true mat.group() returns mat.group(0) :) - pshemo

You need to escape the first '+' like this:


The + means "literally find a + in the string" instead of "find this character 1 or more times"

Respondido 29 ago 12, 14:08

String#contains takes a string as a parameter, not a regex. - Assylias

I am assuming that you would like to either match the string or else maybe extract the digits in the middle? In yout case, the problem is that the + us a special character, thus you need to escape it like so: \\+, so your regex becomes \\+[0-9]+now.

As for your second problem, the .contains method takes a string, not a regular expression, so your code will not work.

    String str = "+124now";
    Pattern p = Pattern.compile("\\+(\\d+)now");
    Matcher m = p.matcher(str);

    while (m.find())

In this case, I have extracted the digits just in case this is something you are after.

Respondido 29 ago 12, 14:08

El método contains does not interpret its argument as a regular expression. Use the method matches instead. You have to escape the + también, así:

if (myString.matches("\\+\\d+now"))

Respondido 29 ago 12, 14:08

Since you said the string always starts with + and always ends with now why not check that this is true. If not then something is wrong.

    String[] vals = {"+124now", "+78now", "-124now", "+124new"};

    for (String s : vals) {
        if (s.matches("^\\+(\\d+)now$")) {
            System.out.println(s + " matches.");
        } else {
            System.out.println(s + " does not match.");

Of course, if you want to capture the number then use a matcher like npinti suggests.

EDIT: Here's how to get the number:

    Pattern p = Pattern.compile("^\\+(\\d+)now$");
    for (String s : vals) {
        Matcher m = p.matcher(s);
        if (m.matches()) {
            System.out.println(s + " matches and the number is: " + m.group(1));
        } else {
            System.out.println(s + " does not match.");

Respondido 29 ago 12, 14:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.