Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding right regex pattern

Tags:

java

So let's say I have an array of Strings of the following type

something **foo**garblesomething somethingelse
something **foobar**blahblah somethingelse
and so on

So essentially data are triples

Now, in the middle part of the string

Notice keywords "foo" and "foobar"

What I want to do is:

if "foo" in string:
    return 1
else if "foobar" in string:
    return 2
else -1

How do I do this in Java?

like image 952
frazman Avatar asked Jul 23 '26 14:07

frazman


1 Answers

Just use String#contains(String)

if (str.contains("foobar"))
    return 2;
else if (str.contains("foo"))
    return 1;
else
    return -1;

Important to check for foobar before foo otherwise it will return 1 for both cases.

like image 57
anubhava Avatar answered Jul 26 '26 04:07

anubhava