How to pass variable in regex-Java to make a script dynamic? - java

I have this function written in Java-Selenium and wondering how I can pass a variable(key) in place of evar90? Your support will be appreciated a lot.
public void verifyXyz(String key,String expectedResult) {
selenium.waitForPageLoad();
String resultString = selenium.executeJSCall("localStorage.getItem('s.tl-data')");
String matchedString = "";
String variable = "key";
Pattern pattern = Pattern.compile("(eVar90=[^:]*)");
Matcher matcher = pattern.matcher(resultString);
if (matcher.find()) {
matchedString = matcher.group();
}
Assert.assertTrue("Product variable does not match expected value \n"
+ "Expected: " + expectedResult + " \n"
+ "Actual: " + matchedString
, matchedString.equals(expectedResult));
}
Thank you in Advance!

Related

Split the string with different length

I have a String String s = " ABCD 1122-12-12", i.e <space or single digit number>+<any string>+<space>+<date in YYYY-mm-dd> format.
What could be the regex for String.split method or any other utility methods to split the above string into three parts
[0] = <space or single digit number> = " "
[1] = <string> = "ABCD"
[2] = <date in YYYY-mm-dd> = "1122-12-12"
The regex ( |\d)(.+) (\d{4}-\d{2}-\d{2}) should do the job.
String input = " ABCD 1122-12-12";
Pattern pattern = Pattern.compile("( |\\d)(.+) (\\d{4}-\\d{2}-\\d{2})");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String spaceOrDigit = matcher.group(1);
String string = matcher.group(2);
String date = matcher.group(3);
System.out.println("spaceOrDigit = '" + spaceOrDigit + "'");
System.out.println("string = '" + string + "'");
System.out.println("date = '" + date + "'");
}
Output:
spaceOrDigit = ' '
string = 'ABCD'
date = '1122-12-12'

Java split method

I am trying to split an inputted number such as (123) 456-7890.
String [] split = s.split(delimiters);
I have been searching the web for ways of delimiting the area code inside the set of the parentheses but I haven't found anything that works for my case. I do not know if the array is messing up with it printing either. The array is not required but I did not know what else to do since it is required to use the split method.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld{
public static void main(String[] args){
String phoneNumber = "(123)-456-7890";
String pattern = "\\((\\d+)\\)-(\\d+)-(\\d+)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(phoneNumber);
if (m.find())
System.out.println(m.group(1) + " " + m.group(2) + " " + m.group(3));
}
}
You can try it here.
If I understand your question, you could use a Pattern like (XXX) XXX-XXXX where X is a digit. You can also use {n} to require n occurences. You group with (). Something like,
String str = "(123) 456-7890";
Pattern p = Pattern.compile("\\((\\d{3})\\) (\\d{3})-(\\d{4})");
Matcher m = p.matcher(str);
if (m.matches()) {
String areaCode = m.group(1);
String first3digits = m.group(2);
String last4digits = m.group(3);
System.out.printf("(%s) %s-%s%n", areaCode, first3digits,
last4digits);
}
Gives your requested output of
(123) 456-7890
or, if you must use split you might first remove the ( and ) with a call to replaceAll and something like
String str = "(123) 456-7890";
String[] arr = str.replaceAll("[()]", "").split("[ -]");
System.out.printf("(%s) %s-%s%n", arr[0], arr[1], arr[2]);
which also gives your requested output of
(123) 456-7890
this works:
String s= "(123) 456-7890";
String[] parts = s.split("[()\\- ]");
System.out.println("(" + parts[1] + ") " + parts[3] + "-" + parts[4]);
If you must use the split method:
String s= "(123) 456-7890"
String[] split = s.split("[()-]");
System.out.println("(" + split[1] + ")" + split[2] + "-" + split[3]);

Filter out the price or cost in the Java

I have got ((?:[0-9]{1,3}[\.,]?)*[\.,]?[0-9]+) to filter out the prices in a string on java so I put them like this:
public static final String new_price = "((?:[0-9]{1,3}[\\.,]?)*[\\.,]?[0-9]+)";
final Pattern p = Pattern.compile(new_price, 0);
final Matcher m = p.matcher(label);
if (m.matches()) {
Log.d(TAG, "found! good start");
if (m.groupCount() == 1) {
Log.d(TAG, "start match price" + " : " + m.group(0));
}
if (m.groupCount() == 2) {
Log.d(TAG, "start match price" + " : " + m.group(1));
}
}
I got the sample working on http://www.regexr.com/ but it never found the matches on the run time. Any idea??
Instead of using matches() you should run m.find() which searches for the next match (this should be done in a while loop!):
String new_price = "((?:[0-9]{1,3}[\\.,]?)*[\\.,]?[0-9]+)";
String label = "$500.00 - $522.30";
final Pattern p = Pattern.compile(new_price, 0);
final Matcher m = p.matcher(label);
while (m.find()) {
System.out.println("found! good start");
if (m.groupCount() == 1) {
System.out.println("start match price" + " : " + m.group(0));
}
if (m.groupCount() == 2) {
System.out.println("start match price" + " : " + m.group(1));
}
}
OUTPUT
found! good start
start match price : 500.00
found! good start
start match price : 522.30

how to filter out some substrings with regex effectively?

i want to filter out srcport and dstport from the input string. here is the code i tried:
String input = "2014<>10.100.2.3<><189>date=2014-01-16,time=11:26:14,devname=B3909601569,devid=B3909601569,logid=000013,type=traffic,srcip=192.168.192.123,srcport=2072,srcintf=port2,dstip=10.180.1.105,dstport=3206,dstintf=port1,sessionid=121543,status=close,policyid=196,service=MYSQL,proto=6,duration=10,sentbyte=3910,rcvdbyte=175085,sentpkt=74,rcvdpkt=132";
Pattern p = Pattern.compile("(srcport=)(\\d+).[\\s]?(dstport=)(\\d+)");
Matcher m = p.matcher(input);
StringBuffer result=new StringBuffer();
while (m.find()) {
System.out.println("Srcport: " + m.group(2) + " & ");
System.out.println("Dstport: " + m.group(4));
}
System.out.println(result);
but its not showing any output. Is there a mistake in the regex
Pattern p = Pattern.compile("(srcport=)(\\d+).[\\s]?(dstport=)(\\d+)");
or the println lines
System.out.println("Srcport: " + m.group(2) + " & ");
System.out.println("Dstport: " + m.group(4));"
any suggestions will be highly appreciated.
See following changes to both the regex and the captured groups:
String input = "2014<>10.100.2.3<><189>date=2014-01-16,time=11:26:14,devname=B3909601569,devid=B3909601569,logid=000013,type=traffic,srcip=192.168.192.123,srcport=2072,srcintf=port2,dstip=10.180.1.105,dstport=3206,dstintf=port1,sessionid=121543,status=close,policyid=196,service=MYSQL,proto=6,duration=10,sentbyte=3910,rcvdbyte=175085,sentpkt=74,rcvdpkt=132";
Pattern p = Pattern.compile("srcport=(\\d+).*?dstport=(\\d+)"); // update regex
Matcher m = p.matcher(input);
StringBuffer result=new StringBuffer();
while (m.find()) {
System.out.println("Srcport: " + m.group(1)); //print groups 1 + 2
System.out.println("Dstport: " + m.group(2));
}
System.out.println(result);
You forgot to use or(|) in your regex
srcport=(\\d+)|dstport=(\\d+)
Your code would be
while (m.find())
{
if(m.group().startsWith("srcport"))
System.out.println("Srcport: " + m.group(1) + " & ");
else
System.out.println("Dstport: " + m.group(1));
}
Try this :
Pattern p = Pattern.compile("srcport=(\\d+)|dstport=(\\d+)");
Try the below code. I have run this in my system and it it working fine.
String input = "2014<>10.100.2.3<><189>date=2014-01-16,time=11:26:14,devname=B3909601569,devid=B3909601569,logid=000013,type=traffic,srcip=192.168.192.123,srcport=2072,srcintf=port2,dstip=10.180.1.105,dstport=3206,dstintf=port1,sessionid=121543,status=close,policyid=196,service=MYSQL,proto=6,duration=10,sentbyte=3910,rcvdbyte=175085,sentpkt=74,rcvdpkt=132";
Pattern p = Pattern.compile("(srcport=)(\\d+)((.*)?)(dstport=)(\\d+)(\\.)*");
Matcher m = p.matcher(input);
StringBuffer result=new StringBuffer();
while (m.find()) {
System.out.println(m.group());
System.out.println("Srcport: " + m.group(2) );
System.out.println("Dstport: " + m.group(6));
}

java regex to match variable

I have the name of a java variable in a string. I want to replace it with the letter x. How can I do this java, and make sure that other words in the string are not replaced ?
For example, say my variable is res, and my string is "res = res + pres + resd + _res. I want the string to become x = x + pres + resd + _res.
You can use a word boundary to only capture whole words:
String s = "res = res + pres + resd + _res";
String var = "res";
System.out.println(s.replaceAll("\\b" + var + "\\b", "x"));
outputs x = x + pres + resd + _res
You can use the \b metacharacter to match a word boundary. (Bear in mind you'll need to use doule backslashes to escape this in Java.)
So you can do something like the following:
final String searchVar = "res";
final String replacement = "x";
final String in = "res = res + pres + resd + _res";
final String result = in.replaceAll("\\b" + searchVar + "\\b", replacement);
System.out.println(result);
// prints "x = x + pres + resd + _res"

Categories

Resources