Is there any java function act like 'LIKE' statement in SQL? - java

I want to find a function in java that can check if string contain pattern "%A%B%" just like 'LIKE' statement in SQL. This function will return true if the string contain the pattern and false if not.
Can anyone suggest any class, function or line of code? Thank you!

Regular expression. Learn more here: https://docs.oracle.com/javase/tutorial/essential/regex/
The easiest way of calling it is using String.matches(String regex)
If you want to check the same regular expression more often, it's better to precompile it and use a Pattern.
A typical invocation sequence is then
Pattern p = Pattern.compile(".*A.*B.*"); // you keep this stored for re-use
Matcher m = p.matcher("BARBARIAN");
boolean b = m.matches();
There is a good Online Regex Tester and Debugger tool, where you can check your regular expression.

Pattern.compile(".*A.*B.*").matches(input)
will return true if input contains an A followed by a B.

Related

Regular expression handlers within java

Hi I have a series of regular expressions which I am trying to match to an input string. I want to then pass this string to a handler to complete some function on it based on what regular expression it matched. Is there an eloquent way to do this or is a series of if statements my best option?
You may probably combine your multiple regexps into single one like this: (regex1)|(regex2)|...|(regexN). Once combined regex matches, you may query Matcher object for what group is non-empty and choose function based on this.

Why I must specify whole string in Java regular expression? [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 6 years ago.
Suppose, I have a string:
String str = "some strange string with searched symbol";
And I want to search in it some symbols, suppose it will be "string". So we have a following:
str.matches("string"); //false
str.matches(".*string.*"); //true
So, as stated in the title, why I must specify whole string in Java regular expression?
Java documentation says:
public boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
It doesn't says
Tells whether or not this whole string matches the given regular expression.
For example, in the php it would be:
$str = "some strange string with searched symbol";
var_dump(preg_match('/string/', $str)); // int(1)
var_dump(preg_match('/.*string.*/', $str)); //int(1)
So, both of the regex's will be true.
And I think this is correct, because if I want to test whole string I would do str.matches("^string$");
PS: Yes, I know that is to search a substring, simpler and faster will be to use str.indexOf("string") or str.contains("string"). My question regards only to Java regular expression.
UPDATE: As stated by #ChrisJester-Young (and #GyroGearless) one of the solutions, if you want to search regex that is part of a subject string, is to use find() method like this:
String str = "some strange string with searched symbol";
Matcher m = Pattern.compile("string").matcher(str);
System.out.println(m.find()); //true
matches always matches the whole input string. If you want to allow substrings to match, use find.
As the documentation you suggest,
public boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
What it means is whether that string matches with the given regex. i.e. matches verifies whether your string is an instance of the given regex.
not whether it contains a substring which is an instance of the given regex. As Chris suggested you can use find instead or for your problem you can use contains.
Yes, as you already know (now) that matches() will return true only for the complete string.
But, in your update, you have stated that:
As stated by #ChrisJester-Young (and #GyroGearless) the only solution, if you want to search regex that is part of a subject string, is to use find() method...
I would like to say that using find() is not the only solution. There is at least one more, which I know :
String str = "some strange string with searched symbol";
boolean found = str.split("string").length>1;
System.out.println(found);
This prints true. And this will work for all regular expressions. Though this is not the way to do it, and is instead a hack.
There may be many more solutions.

Using regex in verifyEquals()

I have selenium test written in java, I want to use regular expression in verifyEquals() to match with
selenium.getText(id="something")
can I use it like this?
verifyEquals("regexp: Regular Expression", selenium.getText("id=something"))
No.
But you can do this:
verifyTrue(selenium.getText(id="something").matches("someregex"));
NB: The regex you pass to matches() must match the whole string to return true.

How can I provide an OR operator in regular expressions?

I want to match my string to one sequence or another, and it has to match at least one of them.
For and I learned it can be done with:
(?=one)(?=other)
Is there something like this for OR?
I am using Java, Matcher and Pattern classes.
Generally speaking about regexes, you definitely should begin your journey into Regex wonderland here: Regex tutorial
What you currently need is the | (pipe character)
To match the strings one OR other, use:
(one|other)
or if you don't want to store the matches, just simply
one|other
To be Java specific, this article is very good at explaining the subject
You will have to use your patterns this way:
//Pattern and Matcher
Pattern compiledPattern = Pattern.compile(myPatternString);
Matcher matcher = pattern.matcher(myStringToMatch);
boolean isNextMatch = matcher.find(); //find next match, it exists,
if(isNextMatch) {
String matchedString = myStrin.substring(matcher.start(),matcher.end());
}
Please note, there are much more possibilities regarding Matcher then what I displayed here...
//String functions
boolean didItMatch = myString.matches(myPatternString); //same as Pattern.matches();
String allReplacedString = myString.replaceAll(myPatternString, replacement)
String firstReplacedString = myString.replaceFirst(myPatternString, replacement)
String[] splitParts = myString.split(myPatternString, howManyPartsAtMost);
Also, I'd highly recommend using online regex checkers such as Regexplanet (Java) or refiddle (this doesn't have Java specific checker), they make your life a lot easier!
The "or" operator is spelled |, for example one|other.
All the operators are listed in the documentation.
You can separate with a pipe thus:
Pattern.compile("regexp1|regexp2");
See here for a couple of simple examples.
Use the | character for OR
Pattern pat = Pattern.compile("exp1|exp2");
Matcher mat = pat.matcher("Input_data");
The answers are already given, use the pipe '|' operator. In addition to that, it might be useful to test your regexp in a regexp tester without having to run your application, for example:
http://www.regexplanet.com/advanced/java/index.html

getting matching regular expressions in java

String.split(String regex) splits the string around a given regular expression and returns an String array. But I am interested in the regex matches and would like them to be returned as string array instead of strings around them.
For example,
In case of trival regex like ":" it probably wouldn't matter. But there are regexes which would match a particular date in a paragraph and I would like to get all these dates which may be different each time. I checked the jdk api but couldn't find any such methods. Is there any method that I can make use of?. Any help would much appreciated.
Take a look at java.util.regex package Matcher and Pattern classes:
http://download.oracle.com/javase/6/docs/api/java/util/regex/package-summary.html
Just use the Java regular expression API
Pattern pat = Pattern.compile("\\d");
Matcher mat= pat.matcher("Foo99Bar66Baz");
while(mat.find()) {
System.out.println(mat.group());
}
You can find simple but quite comprehensive examples for startup in the following link
http://www.vogella.de/articles/JavaRegularExpressions/article.html
Also Pattern and Matcher usage example in:
http://www.vogella.de/articles/JavaRegularExpressions/article.html#regexjava

Categories