I am implementing as automation script and following BDD frame work with selenium webdriver.
Acceptance Criteria:
Scenario: Members name
Given that the web page is displayed
When the user clicks anywhere on the member row
Then member First Name, Middle Initial, Last Name will be displayed
And Member First Name, Middle Initial, Last name display in Camel case
Would you pleas let me know how to validate the Camel case for displayed information in web page? Like "Jhon D Hamton".
By using WordUtils in the Apache Commons lang library:
Specifically, the capitalizeFully(String str, char[] delimiters) method should do the job:
String blah = "LORD_OF_THE_RINGS";
assertEquals("LordOfTheRings", WordUtils.capitalizeFully(blah, new char[]{'_'}).replaceAll("_", ""));
Resource Link:
What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?
Well, assuming you can grab your string, I'd split it then check that the first letter is uppercase:
boolean isCamelCase(String s) {
String[] split_string = s.split(" ");
for(int i=0;i<split_string.length;i++) {
if(!Character.isUpperCase(split_string[i].charAt(0))){
return false;
}
}
return true;
}
If you know how, or are able to get the entire name as a string, you can use the following Regex to assert that it is camel case:
[A-Z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*
This matches strings that start with an uppercase letter, contain only letters and numbers, and contain at least one lowercase letter and at least one other uppercase letter.
For Java, you want to call the String class's matches() method, which returns true or false:
boolean doesItMatch = yourString.matches('theRegexAbove');
Related
I am writing a setter for a domain class. What is being saved is an xml that is a response from a web service. It includes the first and last name of the user but that information needs to be masked. So i am trying to accomplish that using regex.
I wrote the following setter method:
public void setOnlineRetroCreditResponse(String xml) {
xml.replaceAll (/(?<=lastName=)([^\s]+)/){lastName ->
lastName[0].replace ( lastName[1], "X".multiply (lastName[1].size()))
}
onlineRetroCreditResponse = xml
}
I am expecting a sting like this one: "FFPAccountNumber2=12345 lastName=Doe" to be substituted and saved to the databse like this "FFPAccountNumber2=12345 lastName=XXX" but that is not working as expected. I tested my regex using different online like this one https://www.freeformatter.com/java-regex-tester.html and that does not seem to be the issue.
Any ideas would be appreciated.
There are two things: 1) you do not assign the replaced value back to the xml variable and 2) you are replacing the match explicitly while you can just do the necessary modifications to the value you have captured to return it.
Actually, you do not even need to capture the non-whitespace character chunk, you may access the whole match in the replaceAll callback. Also, you can use \S instead of [^\s].
Use
public void setOnlineRetroCreditResponse(String xml) {
onlineRetroCreditResponse = xml.replaceAll(/(?<=lastName=)\S+/){lastName ->
"X".multiply(lastName[0].size())
}
}
I am getting stuck in this situation.
public void findListOfPattern(){
String text = "abce1213abcd231asdf";
String find = "1213|231|1232";
Pattern part = Pattern.compile(find);
Matcher mat = part.matcher(text);
System.out.println(mat.find()); //True
}
Able to get true result if any of string in find get match.
I want list of matcher from text.
There text can large with more find string and also find string can more.
In find : 1213,231,1232 are separates.
Result should be like :- 1213,231
You need to invoke mat.group() to return the desired match.
Typically you'd loop until mat.find() returns true and print all matches successively by invoking mat.group().
You can then build your expected result String by concatenating the outcome of mat.group() as you wish, e.g. with a StringBuilder.
Notes
API here.
You need to invoke Matcher#find in order for Matcher#group to yield any result and not throw IllegalStateException
Your Pattern only has the default group. If you'd used parenthesis or named groups (from Java 7), you could also invoke overloads Matcher#group(int group) or Matcher#group(String name).
I have a list of strings: ab10sdj, ba1wqa, cd03asce, dfasc, etc. I'm looking to get the group of digits from the strings that start either with ab, or with ba.
So if a string starts with ab/ba, I need the group of digits right after them. If there a way for me to achieve this via a java matcher/regex?
Apologies for posting this as an answer but my current reputation level doesn't allow me 'comment' ... yet :)
As apposed to simply giving an answer to a relatively simple problem (without insulting your intelligence), I can help with diagnosing a solution. Try thinking the problem through in your mind, step by step. That is:
How do you test for the first 2 characters in a string?
If the test to point 1 passes (IE, they are ab or ba), how do you then process the rest of the string to test for 'digits' only?
How do you stop processing the 'digits' when you reach a non-digit?
Once you have your ab/ba prefix, only extracted the 'digits' immediately following the test condition, how will you handle the digits extracted?
Before considering placing the digits in a primitive type, you may wish to consider how many digits one may expect?
All the best with your code!
You can try regex like this :
public static void main(String[] args) {
// ab10sdj, ba1wqa, cd03asce, dfasc
String s1 = "ab10sdj";
String s2 = "ba1wqa";
String s3 = "cd03asce";
String s4 = "dfasc";
String pattern = "^(ab|ba)(\\d+).*";
System.out.println(s1.replaceAll(pattern, "$2")); // output 10
System.out.println(s2.replaceAll(pattern, "$2")); // output 1
System.out.println(s3.replaceAll(pattern, "$2")); // output cd03asce i.e, no change
System.out.println(s4.replaceAll(pattern, "$2")); // output dfasc i.e, no change
}
I am writing some auto test using Web Driver to find text on page.
I have variable address - with value Some one, Address
And text on page Some One, Address
Difference only in One and one
but my regex not working
...get(LINK, "regexp:(?i)("+address+")").exists(); // should return boolean
Please, help me to resolve this problem,
Thanks
Try this:
String base = "Some One, Address";
System.out.println(base.matches("(?i:.*address.*)"));
I am having regex expression problem. need helps from regex experts!
It's fairly simple but I can't get it to work.
I know if I want to check the starting of a text, I should use ^
and ending of the text, I should use $
I want to replace [quote] to <a>quote</a>.
This doesn't seems to work..
String test = "this is a [quote]"
test.replaceAll("^\\[", "<a>");
test.replaceAll("\\]$", "</a>");
I want the string to become "this is a <a>quote</a>"..
If you want to replace [ and ] with pair, you need to replace them in one time.
String test = "this [test] is a [quote]";
String result = test.replaceAll("\\[([^\\]]+)\\]", "<a>$1</a>");
^ implies that you are looking for something at the beginning of the string. However [ does not appear at the beginning of the string, so you will not have a match. Just do:
test.replaceAll("\\[", "<a>");
test.replaceAll("\\]", "</a>");
Also, you cannot modify a String in-place. you'll have to assign the output to something. You can do:
test = test.replaceAll("\\[", "<a>").replaceAll("\\]", "</a>");
That is if you still want to use the variable test.