Using regex in verifyEquals() - java

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.

Related

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

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.

Why does the following regex not work for Java?

I want to delete a word and all its trailing whitespace.
Here is my regex:
item.getName().replace(word + "(\\s*)?", "");
I tested this statement by running:
item.getName().replace(word, "");
This executes successfully, albeit with extra whitespaces. So the error must be due to "(\\s*)?" part. Is it because I did not escape the slash correctly? Or does Java not recognize something in that regex?
replace uses a String literal as its first argument. Use replaceAll instead
String.replace method does not take regular expressions. I believe you'd have to use replaceAll in orer to use regular expression. Also, regular expressions are a general grammar that expresses a certain pattern of String rather than a particular instances that contain certain words. You can't mix a word with a regular expression such way.

string.matches(regex) returns false, although I think it should be true

I am working with Java regular expressions.
Oh, I really miss Perl!! Java regular expressions are so hard.
Anyway, below is my code.
oneLine = "{\"kind\":\"list\",\"items\"";
System.out.println(oneLine.matches("kind"));
I expected "true" to be shown on the screen, but I could only see "false".
What's wrong with the code? And how can I fix it?
Thank you in advance!!
String#matches() takes a regex as parameter, in which anchors are implicit. So, your regex pattern will be matched at the beginning till the end of the string.
Since your string does not start with "kind", so it returns false.
Now, as per your current problem, I think you don't need to use regex here. Simply using String#contains() method will work fine: -
oneLine.contains("kind");
Or, if you want to use matches, then build the regex to match complete string: -
oneLine.matches(".*kind.*");
The .matches method is intended to match the entire string. So you need something like:
.*kind.*
Demo: http://ideone.com/Gb5MQZ
Matches tries to match the whole string (implicit ^ and $ anchors), you want to use contains() to check for parts of the string.

String.replace() and regexp. Will regexp symbols in pattern affect or not?

Please correct me if I am wrong.
I want to replace substring in a string in java. And I want to use String.replace(CharSequence target, CharSequence replacement) method.
I do not use regular expressions in target substring and I think this method is a good choice.
This method will work properly even there will be special regexp symbols in target substring and it will just ignore regexp format and treat target substring as a regular string.
Am I right?
Thank you.
Yes, if you use replace the arguments will be treated as ordinary strings, not regular expressions.
If you want to replace using an regular expression you need to use replaceAll.
Yes you are correct: String.replace does not use regex. It replaces a literal with another literal.

Java Regex won't match, any explanations?

The regex String :
"[Ff][uU][Nn][Cc] "
Matches input:
"fUnC "
But not:
"func across( a, b )"
And I don't understand why...
I'm testing my expressions here:
http://www.regexplanet.com/simple/index.html
I figured out that I (dumbly) needed my regex to be "[Ff][uU][Nn][Cc] .*" for a match.
SOLVED: Don't use the convenience method Pattern.Matches(regex, input) if you are looking for what amounts to a submatch. You should use the Matcher.find() method instead.
When I use the regex tester you link to, I see that your regex works with find(), but not with matches(). This is what I would expect -- find() just looks for a regex hit within the target string, while matches() always tries to match the entire string.
"[Ff][uU][Nn][Cc].*" may help...
It can be.... it's working fine. But your strings in there and you'll see MATCHES is false, but replaceFirst and ReplaceAll work fine.
If you want MATCHES to be true
add a * at the end
Have you also tried using the regex tester, ignoring case? There should be a way to turn on case insensitivity in the Java regex matcher.

Categories