Adam Varhegyi
Adam Varhegyi

Reputation: 9894

Regular expression in java? I'm stuck

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

+124now

+78now

+45now

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?

Upvotes: 0

Views: 255

Answers (6)

km1
km1

Reputation: 2443

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.");
        }
    }

Upvotes: 1

João Silva
João Silva

Reputation: 91329

Use String#matches() instead:

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

Also, + is a special regex character, that's why you need to escape it.

If you need to capture the numbers, use Pattern and Matcher:

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

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

Upvotes: 9

npinti
npinti

Reputation: 52185

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())
    {
        System.out.println(m.group(1));         
    }

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

Upvotes: 2

Smi
Smi

Reputation: 14336

The method contains does not interpret its argument as a regular expression. Use the method matches instead. You have to escape the + as well, like this:

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

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Try this......

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

while(mat.find()){

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

 }

Upvotes: 6

DankMemes
DankMemes

Reputation: 2137

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

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

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

Upvotes: 3

Related Questions