I have a string like this:
String source = "https://1116.netrk.net/conv1?prid=478&orderid=[6aa3482b-519b-4127-abee-debcd6e39e96]"
I want to extract orderid which is inside [ ]. I wrote this method:
public String extractOrderId(String source)
{
Pattern p = Pattern.compile("[(.*?)]");
Matcher m = p.matcher(source);
if (m.find())
return m.group(1);
else
return "";
}
But I always get Exception
java.lang.IndexOutOfBoundsException: No group 1
Any idea what's wrong? Thanks
You need to escape brackets:
Pattern p = Pattern.compile("\\[(.*?)\\]");
Aside from using a regex you could use the URL class to extract the query:
URL url = new URL("https://1116.netrk.net/conv1?prid=478&orderid=[6aa3482b-519b-4127-abee-debcd6e39e96]");
String query = url.getQuery();
At that point the value of query is prid=478&orderid=[6aa3482b-519b-4127-abee-debcd6e39e96]. From there you can easily extract the orderid by a combination of indexOf and substring.
String searchFor = "orderid=[";
int fromIndex = query.indexOf(searchFor);
int toIndex = query.indexOf("]", fromIndex);
//6aa3482b-519b-4127-abee-debcd6e39e96
String orderId = query.substring(fromIndex+searchFor.length(), toIndex);
Your RegEx is lack of \\
Pattern p = Pattern.compile("\\[(.*?)]");
You need to use escape characters for [] , try the following
Pattern p = Pattern.compile("\\[(.*)\\]");
You have used [(.*?)]. Please refer this for the meaning of square brackets.
So in this case, you need to define the regex for the character [ and ] as well. So you need to escape those characters from the Pattern.compiler.
The following will match the requirement that you want.
Pattern p = Pattern.compile("\\[(.*?)\\]");
Related
I have the following text ArtClass_Private_Method.boo(I)Z. I would like to return only the string after the dot and before parentheses. In this case, the word boo should only be returned. For that, I tried the following:
String x = "ArtClass_Private_Method.boo(I)Z";
String[] wo = x.split("\\.");
System.out.println(wo[1]);
But this only returns boo(I)Z and I don't know how to avoid (I)Z.
Can someone help me fixing that?
Use indexOf to find the .:
int dotPos = x.indexOf('.');
Use indexOf to find the following (:
int parenPos = x.indexOf('(', dotPos);
Then take the substring between (adding 1 to dotPos so you start after it):
String between = x.substring(dotPos + 1, parenPos);
(This assumes the . and the ( can be found. I'll leave you to handle the case when they're not).
You can use the regex (?<=\\.).+(?=\\()
String x = "ArtClass_Private_Method.boo(I)Z";
Pattern pattern = Pattern.compile("(?<=\\.).+(?=\\()");
Matcher matcher = pattern.matcher(x);
matcher.find();
System.out.println(matcher.group(0)); // boo
I am trying to get the image name from the following javascript.
var g_prefetch ={'Im': {url:'\/az\/hprichbg\/rb\/WhiteTippedRose_ROW10477559674_1366x768.jpg', hash:'674'}
Problem:
The name of the image is variable. That is, in the above example code the image changes regularly.
Output I want:
WhiteTippedRose_ROW10477559674_1366x768.jpg
and i tried the following regExp :
Pattern p = Pattern.compile("\{\'Im\'\: \{url\:\'\\\/az\\\/hprichbg\\\/rb\\\/(.*?)\.jpg\'\, hash\:\'674\'\}");
//System.out.println(p);
Matcher m=p.matcher(out);
if(m.find()) {
System.out.println(m.group());
}
I don't know too much RegExp so please help me and let me understand the approach.
Thank You
I would use the following regex, it should be fast enough:
Pattern p = Pattern.compile("[^/]+\\.jpg");
Matcher m = p.matcher(str);
if (m.find()) {
String match = m.group();
System.out.println(match);
}
This will match the a full sequence of characters ending with .jpg not including /.
I think that the correct approach will be to check the correct legality of a file name.
Here is a list of not legal characters for Windows: "\\/:*?\"<>|"
for Mac /:
Linux/Unix /;
Here is more complex example assuming format will change , it is mostly designed for legal Window file name:
String s = "{'Im': {url:'\\/az\\/hprichbg\\/rb\\/?*<>WhiteTippedRose_ROW10477559674_1366x768.jpg', hash:'674'}";
Pattern p = Pattern.compile("[^\\/:*?\"<>|]+\\.jpg");
Matcher m = p.matcher(s);
if (m.find()) {
String match = m.group();
System.out.println(match);
}
This will still print
WhiteTippedRose_ROW10477559674_1366x768.jpg
Here you may find a demo
Assuming that the image is always placed after a / and does not contain any /, you can use the following:
String s = "{'Im': {url:'\\/az\\/hprichbg\\/rb\\/WhiteTippedRose_ROW10477559674_1366x768.jpg', hash:'674'}";
s = s.replaceAll(".*?([^/]*?\\.jpg).*", "$1");
System.out.println("s = " + s);
outputs:
s = WhiteTippedRose_ROW10477559674_1366x768.jpg
In substance:
.*? skip the beginning of the string until the next pattern is found
([^/]*?\\.jpg) a group like "xxx.jpg" where xxx does not contain any "/"
.* rest of the string
$1 returns the content of the group
If the String is always of this form, I would simply do:
int startIndex = s.indexOf("rb\\/") + 4;
int endIndex = s.indexOf('\'', startIndex);
String image = s.substring(startIndex, endIndex);
I want to take a string according to my regex in java. Suppose i have a String "R12T12W5P12T5L3"
. And now i want to have something like this : myStr[0]="R12T12",myStr[1]="W5P12",myStr[2]=T5L3. I want to have my regex first a character then a number then again a character and last a number.
How can i do that?
String s="R12T12W5P12T5L3";
String regex = "([A-Z]\\d+){2}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while(m.find()){
System.out.println(m.group(0));
}
this will print
R12T12
W5P12
T5L3
you can put them into a list and convert into array at the end.
All operations from the regex to the string building, in javascript :
var str = "R12T12W5P12T5L3";
var result = str.split(/(?=[^\d]){2}/).map(function(v,i,a){
return i%2 ? a[i-1]+v+'",' : 'myStr['+(i/2)+']="'
}).join('').slice(0,-1);
Result :
myStr[0]="R12T12",myStr[1]="W5P12",myStr[2]="T5L3"
I am trying to match a regex pattern in Java, and I have two questions:
Inside the pattern I'm looking for there is a known beginning and then an unknown string that I want to get up until the first occurrence of an &.
there are multiple occurrences of these patterns in the line and I would like to get each occurrence separately.
For example I have this input line:
1234567 100,110,116,129,139,140,144,146 http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.&sName=View+All&viewItems=25&subCatView=true ISx20070515x00001a http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=Screen+Refresh+Rate%7C120HZ&sName=View+All&subCatView=true 0 2819357575609397706
And I am interested in these strings:
Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.
Screen+Refresh+Rate%7C120HZ
Assuming the known beginning is filter=**, the regular expression pattern (?:filter=\\*\\*)(.*?)(?:&) should get you what you need. Use Matcher.find() to get all occurrences of the pattern in a given string. Using the test string you provided, the following:
final Pattern p = Pattern.compile("(?:filter=\\*\\*)(.*?)(?:&)");
final Matcher m = p.matcher(testString);
int cnt = 0;
while (m.find()) {
System.out.println(++cnt + ": G1: " + m.group(1));
}
Will output:
1: G1: Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.
2: G1: Screen+Refresh+Rate%7C120HZ**
If i know that I might need other query parameters in the future, I think it'll be more prudent to decode and parse the URL.
String url = URLDecoder.decode("http://www.gold.com/shc/s/c_10153_12605_" +
"Computers+%26+Electronics_Televisions?filter=Screen+Refresh+Rate" +
"%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.&sName=View+All&viewItems=25&subCatView=true"
,"utf-8");
Pattern amp = Pattern.compile("&");
Pattern eq = Pattern.compile("=");
Map<String, String> params = new HashMap<String, String>();
String queryString = url.substring(url.indexOf('?') + 1);
for(String param : amp.split(queryString)) {
String[] pair = eq.split(param);
params.put(pair[0], pair[1]);
}
for(Entry<String, String> param : params.entrySet()) {
System.out.format("%s = %s\n", param.getKey(), param.getValue());
}
Output
subCatView = true
viewItems = 25
sName = View All
filter = Screen Refresh Rate|120HZ^Screen Size|37 in. to 42 in.
in your example, there is sometimes a "**" at the end before the "&". but basically, (assuming "filter=" is the start pattern you are looking for) you want something like:
"filter=([^&]+)&"
Using the regular expression (?<=filter=\*{0,2})[^&]*[^&*]+ in java:
Pattern p = Pattern.compile("(?<=filter=\\*{0,2})[^&]*[^&*]+");
String s = "1234567 100,110,116,129,139,140,144,146 http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=**Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.&sName=View+All**&viewItems=25&subCatView=true ISx20070515x00001a http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=**Screen+Refresh+Rate%7C120HZ**&sName=View+All&subCatView=true 0 2819357575609397706";
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
EDIT:
Added [^&*]+ to the end of the regex to prevent the ** from being included in the second match.
EDIT2:
Changed regular expression to use lookbehind.
The regex you're looking for is
Screen\+Refresh\+Rate[^&]*
You could use Matcher.find() to find all matches.
are you looking for a string that follows with "filter=" and ignores the first "*" and is end with the first "&".
your can try the following:
String str = "1234567 100,110,116,129,139,140,144,146 http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=**Screen+Refresh+Rate%7C120HZ%5EScreen+Size%7C37+in.+to+42+in.&sName=View+All**&viewItems=25&subCatView=true ISx20070515x00001a http://www.gold.com/shc/s/c_10153_12605_Computers+%26+Electronics_Televisions?filter=**Screen+Refresh+Rate%7C120HZ**&sName=View+All&subCatView=true 0 2819357575609397706";
Pattern p = Pattern.compile("filter=(?:\\**)([^&]+?)(?:\\**)&");
Matcher matcher = p.matcher(str);
while(matcher.find()){
System.out.println(matcher.group(1));
}
I have a string
abc.xyz.qweweer.cccc
This is actually going to be a Java package name.
I am trying to find out the last string using reg exp, in the above example cccc is the last String.
Basically I am trying to find out the class name from the package string.
How to find out through Java
Given a string pkg = "abc.xyz.qweweer.cccc" you can solve it like this:
Using indexOf:
int index = pkg.lastIndexOf('.');
String lastPart = index == -1 ? pkg : pkg.substring(index + 1);
Using regular expressions with Matcher:
Matcher m = Pattern.compile("[^.]*$").matcher(pkg);
String lastPart = m.find() ? m.group() : null;
Using split (variation of RMT's answer):
String[] names = pkg.split("\\.");
String lastPart = names[names.length - 1];
Why not just split on the "."
String[] names = packageName.split(".");
String className = names[names.length-1];
Do you really want to use regex? You could do a str.substring (str.lastIndexOf (".") + 1) to get the classname.
Alt1 - Extract desired
String packageName = ...
Matcher m = Pattern.compile("[a-zA-Z]+$").matcher(packageName);
if (m.find()) {
m.group(); // your match
}
Alt2 - Remove undesired
You could also try the somewhat less verbose approach:
String result = packageName.replaceAll(".*\\.", "");
You can use Apache StringUtils substringAfterLast for this.
StringUtils.substringAfterLast("abc.xyz.qweweer.cccc", ".")
Regex is not required for this.