Escape regex String in Java - java

What is the proper way to escape this String 0x\w+:0x\w+[^][]*\K\[(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)\] to be used as a Java String variable.
IntelliJ escapes this automatically into (when pasted):
String pattern = "0x\\w+:0x\\w+[^][]*\\K\\[(\\-?\\d+(\\.\\d+)?),\\s*(\\-?\\d+(\\.\\d+)?)\\]";
However, this is causing compiler error:
java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 18

\K is not valid regex. Not sure what you are trying to match with it. If you want a capitol K character, just put K

Here's what worked for me:
\[\["0x\w+:0x\w+(.+?[\d+,\d+]]])
And here's the complete code (a bit long solution):
String arrayPatternString = "\\[\\[\"0x\\w+:0x\\w+(.+?[\\d+,\\d+]]])";
Pattern arrayPattern = Pattern.compile(arrayPatternString);
Matcher arrayMatcher = arrayPattern.matcher(responseBody);
while(arrayMatcher.find()) {
String matched = arrayMatcher.group();
String g = "(\\-?\\d+(\\.\\d+)?),\\s*(\\-?\\d+(\\.\\d+)?)";
Pattern gPattern = Pattern.compile(g);
Matcher gMatcher = gPattern.matcher(matched);
while(gMatcher.find()) {
String gMatched = gMatcher.group();
String[] s = gMatched.split(",");
Double lat = Double.valueOf(s[0]);
Double lon = Double.valueOf(s[1]);
System.out.println("Lat: " + lat);
System.out.println("Lat: " + lon);
map.put("latitude", lat);
map.put("longitude", lon);
}
}

Related

How to get value of optional parameters from a url with regex in java

I have some uris which I want to extract parameters if they are exist, I come up with this code. Can someone point me to fix regex to success.
cityId and countryId works as expected but Cant get only numbers after word '-a-'
Regex
// "/city/berlin-a-10284?cityId=123456&countryId=4545"
// "/city/berlin-a-10284"
// "/city/berlin-a-10284?cityId=123456"
// "/city/berlin-a-10284?countryId=4545"
private String ValueExtractor(String url, String searchWord) {
String regex = "(?<=" + searchWord + ").*?(?=&|$)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(url);
return matcher.find() ? matcher.group() : "";
}
String productId = "";
String cityId = "";
String countryId = "";
if (url.contains("-p-")) {
productId = ValueExtractor(url, "-a-");
}
if (url.contains("cityId")) {
cityId = ValueExtractor(url, "cityId=");
}
if (url.contains("countryId")) {
countryId = ValueExtractor(url, "countryId=");
}
Expected results:
"/city/berlin-a-10284?cityId=123456&countryId=4545"
productId:10284
cityId: 123456
countryId: 4545
"/city/berlin-a-10284"
productId:10284
"/city/berlin-a-10284?cityId=123456"
productId:10284
cityId: 123456
"/city/berlin-a-10284?countryId=4545"
productId:10284
countryId: 4545
Cant get only numbers after word '-a-'
You can use the regex, (?<=-a-)\d+(?=[?&]|$) to retrieve this number.
(?<=-a-)\d+ specifies one or more digits preceded by -a-.
(?=[?&]|$) specifies positive lookahead for ?, or & or end of line.

complex regular expression in Java

I have a rather complex (to me it seems rather complex) problem that I'm using regular expressions in Java for:
I can get any text string that must be of the format:
M:<some text>:D:<either a url or string>:C:<some more text>:Q:<a number>
I started with a regular expression for extracting the text between the M:/:D:/:C:/:Q: as:
String pattern2 = "(M:|:D:|:C:|:Q:.*?)([a-zA-Z_\\.0-9]+)";
And that works fine if the <either a url or string> is just an alphanumeric string. But it all falls apart when the embedded string is a url of the format:
tcp://someurl.something:port
Can anyone help me adjust the above reg exp to extract the text after :D: to be either a url or a alpha-numeric string?
Here's an example:
public static void main(String[] args) {
String name = "M:myString1:D:tcp://someurl.com:8989:C:myString2:Q:1";
boolean matchFound = false;
ArrayList<String> values = new ArrayList<>();
String pattern2 = "(M:|:D:|:C:|:Q:.*?)([a-zA-Z_\\.0-9]+)";
Matcher m3 = Pattern.compile(pattern2).matcher(name);
while (m3.find()) {
matchFound = true;
String m = m3.group(2);
System.out.println("regex found match: " + m);
values.add(m);
}
}
In the above example, my results would be:
myString1
tcp://someurl.com:8989
myString2
1
And note that the Strings can be of variable length, alphanumeric, but allowing some characters (such as the url format with :// and/or . - characters
You mention that the format is constant:
M:<some text>:D:<either a url or string>:C:<some more text>:Q:<a number>
Capture groups can do this for you with the pattern:
"M:(.*):D:(.*):C:(.*):Q:(.*)"
Or you can do a String.split() with a pattern of "M:|:D:|:C:|:Q:". However, the split will return an empty element at the first index. Everything else will follow.
public static void main(String[] args) throws Exception {
System.out.println("Regex: ");
String data = "M:<some text>:D:tcp://someurl.something:port:C:<some more text>:Q:<a number>";
Matcher matcher = Pattern.compile("M:(.*):D:(.*):C:(.*):Q:(.*)").matcher(data);
if (matcher.matches()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
}
System.out.println();
System.out.println("String.split(): ");
String[] pieces = data.split("M:|:D:|:C:|:Q:");
for (String piece : pieces) {
System.out.println(piece);
}
}
Results:
Regex:
<some text>
tcp://someurl.something:port
<some more text>
<a number>
String.split():
<some text>
tcp://someurl.something:port
<some more text>
<a number>
To extract the URL/text part you don't need the regular expression. Use
int startPos = input.indexOf(":D:")+":D:".length();
int endPos = input.indexOf(":C:", startPos);
String urlOrText = input.substring(startPos, endPos);
Assuming you need to do some validation along with the parsing:
break the regex into different parts like this:
String m_regex = "[\\w.]+"; //in jsva a . in [] is just a plain dot
String url_regex = "."; //theres a bunch online, pick your favorite.
String d_regex = "(?:" + url_regex + "|\\p{Alnum}+)"; // url or a sequence of alphanumeric characters
String c_regex = "[\\w.]+"; //but i'm assuming you want this to be a bit more strictive. not sure.
String q_regex = "\\d+"; //what sort of number exactly? assuming any string of digits here
String regex = "M:(?<M>" + m_regex + "):"
+ "D:(?<D>" + d_regex + "):"
+ "C:(?<D>" + c_regex + "):"
+ "Q:(?<D>" + q_regex + ")";
Pattern p = Pattern.compile(regex);
Might be a good idea to keep the pattern as a static field somewhere and compile it in a static block so that the temporary regex strings don't overcrowd some class with basically useless fields.
Then you can retrieve each part by its name:
Matcher m = p.matcher( input );
if (m.matches()) {
String m_part = m.group( "M" );
...
String q_part = m.group( "Q" );
}
You can go even a step further by making a RegexGroup interface/objects where each implementing object represents a part of the regex which has a name and the actual regex. Though you definitely lose the simplicity makes it harder to understand it with a quick glance. (I wouldn't do this, just pointing out its possible and has its own benefits)

regex matcher check in if logic not working

Hi, you can see my code below. I have some strings Country, rank and grank in my code, initially they will be null, but if regex is mached, it should change the value. But even if regex is matched it is not changing the value it is always null. If I remove all if statements and append the string it works fine, but if match is not found it is throwing an exception. Please let me know how can I check this in if logic.
System.err.println(content);
Pattern c = Pattern.compile("NAME=\"(.*)\" RANK");
Pattern r = Pattern.compile("\" RANK=\"(.*)\"");
Pattern gr = Pattern.compile("\" TEXT=\"(.*)\" SOURCE");
Matcher co = c.matcher(content);
Matcher ra = r.matcher(content);
Matcher gra = gr.matcher(content);
co.find();
ra.find();
gra.find();
String country = null;
String Rank = null;
String Grank = null;
if (co.matches()) {
country = co.group(1);
}
if (ra.matches()) {
Rank = ra.group(1);
}
if (gra.matches()) {
Grank = gra.group(1);
}
You have to escape a single \ - use double \\ then it should work.
Tried this?
while (co.find()) {
System.out.print("Start index: " + co.start());
System.out.print(" End index: " + co.end() + " ");
System.out.println(co.group());
}
Personally I can't make your program work with / without the if so it's not a problem of logic but just a problem that it doesn't match the string for me
So I changed it to get something working, maybe you can use it :)
String content = "NAME=\"salut\" RANK=\"pouet\" TEXT=\"text\" SOURCE";
System.out.println(content);
System.out.println(content.replaceAll(("NAME=\"(.*)\"\\sRANK=\"(.*)\"\\sTEXT=\"(.*)\" SOURCE"), "$1---$2---$3"));
Output
NAME="salut" RANK="pouet" TEXT="text" SOURCE
salut---pouet---text

How to Extract text from given string?

I want to extract a perticular image path string from a given string .
The String is http:\localhost:9090\SpringMVC\images\integration-icon.png
Now i want to get only the path after images like
\images\integration-icon.png
i tried this
Pattern pattern = Pattern.compile("SpringMVC");
Matcher matcher = pattern.matcher(str);
System.out.println("Checking");
if (matcher.find()) {
System.out.println(matcher.group(1));
}
how can i get ?
String filename = filepath.substring(filepath.lastIndexOf("\\") + 1);
or (haven't tried and looks somewhat odd)
String filename = filepath.substring(filepath.lastIndexOf("\\", "images\\".length()) + 1);
String string = "http:\localhost:9090\ZenoBusinessStore\images\integration-icon.png";
int index = string.indexOf("images\\");
String output = string.substring(index);
String text = "http:\localhost:9090\SpringMVC\images\integration-icon.png"
String subText = text.subString(text.indexOf("\images"), text.length());
System.out.println(subText);
String in = "http:\\localhost:9090\\ZenoBusinessStore\\images\\integration-icon.png";
String op = in.replace("http:\\localhost:9090\\ZenoBusinessStore", "");
System.out.println(op);
ZenoBusinessStore must be the name of your project which is constant.
Now split the string
String s = "http:\localhost:9090\ZenoBusinessStore\images\integration-icon.png";
String ary = s.split("ZenoBusinessStore");
Now the 2nd element of the array is your image path.
System.out.println(ary[1]);
Use '\\'. It's because backslash is used in escape sequence like '\n'. With a single \ the compiler have no way to know.

Split a string to extract double value out of it

How can I convert String s="3.78 hi bye" to double c=3.78?
Same question for String s="hi 3.78 hi bye" (take only 3.78 while ignoring the text before)
You can use a regex to get rid of all characters that are not a digit or a .:
String s = "hi 3.78 hi bye";
String numberOnly = s.replaceAll("[^0-9\\.]+", "");
double d = Double.parseDouble(numberOnly); //d == 3.78d
You should add some exception handling in case the original string is not properly formatted.
you may try to use regular expression
Pattern doubleValue = Pattern.compile("\\d+\\.\\d+");
Matcher m = doubleValue.match(yourString);
if (m.find()) {
return Double.parseDouble(m.group(0));
}
return null;

Categories