I am using below java program to find list of js files as a Substring.
String str = "jsLib//connect.facebook.net/en_US/fbevents.js , jsLib//connect.facebook.net/en_US/fbevents2.js;";
String patternStr = "(\\/.*?\\.js)";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(html);
if (matcher.find()) {
System.out.println("Count:" + matcher.groupCount());
jsLib = matcher.group(1);
jsLib = jsLib.substring(jsLib.lastIndexOf('/') + 1, jsLib.length());
System.out.println("jsLib:" + jsLib);
}
Regex : I used String patternStr="(\\/.*?\\.js)";
Expected Result : both fbevents.js and fbevents2.js should be matched and part of result
Actual Result : only fbevents.js is matched
You may get all your results using while loop and a regex like [^/]*\.js:
String str = "jsLib//connect.facebook.net/en_US/fbevents.js , jsLib//connect.facebook.net/en_US/fbevents2.js;";
String patternStr = "[^/]*\\.js";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println("jsLib:" + matcher.group());
}
Output:
jsLib:fbevents.js
jsLib:fbevents2.js
See the Java demo and the regex demo.
The [^/]*\.js pattern matches any 0+ chars other than / (with [^/]*) and then a .js substring.
Suppose I have a string like
"resources/json/04-Dec/someName_SomeTeam.json"
In above string I want just "04-Dec" part, this may change to "12-Jan" like this or any date with month with that format. How do I do this?
You can split using / and get the value 2
String text = "resources/json/04-Dec/someName_SomeTeam.json";
String[] split = text.split("\\/");
String result = split[2];//04-Dec
Or you can use patterns with this regex \d{2}\-\[A-Z\]\[a-z\]{2}:
String text = "resources/json/04-Dec/someName_SomeTeam.json";
String regex = "\\d{2}\\-[A-Z][a-z]{2}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println(matcher.group());
}
I want to extract substring of a given string.
the example string is rta=0.037ms;3000.000;5000.000;0; pl=10%;80;100;; rtmax=0.125ms;;;; rtmin=0.012ms;
I want to get only 0.037ms after "rta=" and percent after pl=. I tried to splite the above string by space and then by semicolon. did not work.
String s = "rta=0.037ms;3000.000;5000.000;0; pl=10%;80;100;; rtmax=0.125ms;;;; rtmin=0.012ms;";
Pattern pattern = Pattern.compile("rta=(.*?);.*pl=(.*?);");
Matcher matcher = pattern.matcher(s);
if(matcher.find()){
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
String str = "rta=0.037ms;3000.000;5000.000;0; pl=10%;80;100;; rtmax=0.125ms;;;; rtmin=0.012ms;";
String[] parts = str.split(";");
String part1 = parts[0]; // rta=0.037ms
String part2 = parts[4]; // pl=10%
...
System.out.println(part1.substring(4)); // 0.037ms
System.out.println(part2.substring(4)); // 10%
I am trying to get particular string from the data below.It is too long am here with sharing sample data. From this I have to get the 'france24Id=7GHYUFGty6fdGFHyy56'
am not that much familier with regex.
How can I retreive the string 'france24Id=7GHYUFGty6fdGFHyy56' from above data?
I tried splitting the data using ',' but it is not an effective way.That's why I choose regex.
2016-07-29 12:08:46,260 s=xGuide, [xre-10-pipe#6da05f7a[,connection=WebSocketConnectionWrapper[/1.8.9]]] INFO c=c.ore., - onConnect event payload={minimumVersion='0', applicationName='shell', fetl='555', authenticationToken='6y777', sessionAuthToken='', sessionGUID='null', connectURL='http://section?ruleName=Default', partnerId='hp', nativeDimensions=null, appParams={heartbeatRequest=1, france24Id=7GHYUFGty6fdGFHyy56, service=false, networkBuffer={min=150, max=150, step=0}}, deviceCaps={platform=Mac, receiverType=Native, revisions={protocol=1, auth=1, video=1}, pixelDimensions=[1280, 720]}, forceSource=null, reconnect=false, currentCommandIndex=0, reconnectReason=7, authService=9}
You can get what you want with (france\d+Id)=([a-zA-Z0-9]+),. This will grab your string and dump the two parts of it into platform-appropriate capture group variables (for instance, in Perl, $1 and $2 respectively).
In Java, your code would look a little like this:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public String matchID(String data) {
Pattern r = new Pattern("(france\\d+Id)=([a-zA-Z0-9]+),");
Matcher m = r.matcher(data);
return m.group(2);
}
public static void main(String[] args) {
String str = "2016-07-29 12:08:46,260 s=xGuide, [xre-10-pipe#6da05f7a[,connection=WebSocketConnectionWrapper[/1.8.9]]] INFO c=c.ore., - onConnect event payload={minimumVersion='0', applicationName='shell', fetl='555', authenticationToken='6y777', sessionAuthToken='', sessionGUID='null', connectURL='http://section?ruleName=Default', partnerId='hp', nativeDimensions=null, appParams={heartbeatRequest=1, france24Id=7GHYUFGty6fdGFHyy56, service=false, networkBuffer={min=150, max=150, step=0}}, deviceCaps={platform=Mac, receiverType=Native, revisions={protocol=1, auth=1, video=1}, pixelDimensions=[1280, 720]}, forceSource=null, reconnect=false, currentCommandIndex=0, reconnectReason=7, authService=9}";
String regex = ".*(france24Id=[\\d|\\w]*),.*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){
System.out.println(matcher.group(1));
}
}
You can use Pattern and Matcher classes in Java.
String data = "2016-07-29 12:08:46,260 s=xGuide, [xre-10-pipe#6da05f7a[,connection=WebSocketConnectionWrapper[/1.8.9]]] INFO c=c.ore., - onConnect event payload={minimumVersion='0', applicationName='shell', fetl='555', authenticationToken='6y777', sessionAuthToken='', sessionGUID='null', connectURL='http://section?ruleName=Default', partnerId='hp', nativeDimensions=null, appParams={heartbeatRequest=1, france24Id=7GHYUFGty6fdGFHyy56, service=false, networkBuffer={min=150, max=150, step=0}}, deviceCaps={platform=Mac, receiverType=Native, revisions={protocol=1, auth=1, video=1}, pixelDimensions=[1280, 720]}, forceSource=null, reconnect=false, currentCommandIndex=0, reconnectReason=7, authService=9}";
String regex1 = "france24Id=[a-zA-Z0-9]+"; //this matches france24Id=7GHYUFGty6fdGFHyy56
String regex2 = "(?<=france24Id=)[a-zA-Z0-9]+"; //this matches 7GHYUFGty6fdGFHyy56 or whatever after "france24Id=" and before ','
Pattern pattern1 = Pattern.compile(regex1);
Pattern pattern2 = Pattern.compile(regex2);
Matcher matcher1 = pattern1.matcher(data);
Matcher matcher2 = pattern2.matcher(data);
String result1, result2;
if(matcher1.find())
result1 = matcher1.group(); //if match is found, result1 should contain "france24Id=7GHYUFGty6fdGFHyy56"
if(matcher2.find())
result2 = matcher2.group(); //if match is found, result1 should contain "7GHYUFGty6fdGFHyy56"
You can also try this one:
String str = "france24Id=7GHYUFGty6fdGFHyy56";
Pattern pattern = Pattern.compile("(?<=france24Id=)([a-zA-Z0-9]+)");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println("ID = " + matcher.group());
}
And the result is:
ID = 7GHYUFGty6fdGFHyy56
If the source string contains the pattern, then replace it with something or remove it. One way to do it is to do something like this
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sourceString);
while(m.find()){
String subStr = m.group().replaceAll('something',""); // remove the pattern sequence
String strPart1 = sourceString.subString(0,m.start());
String strPart2 = sourceString.subString(m.start()+1);
String resultingStr = strPart1+subStr+strPart2;
p.matcher(...);
}
But I want something like this
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sourceString);
while(m.find()){
m.group.replaceAll(...);// change the group and it is source string is automatically updated
}
Is this possible?
Thanks
// change the group and it is source string is automatically updated
There is no way what so ever to change any string in Java, so what you're asking for is impossible.
To remove or replace a pattern with a string can be achieved with a call like
someString = someString.replaceAll(toReplace, replacement);
To transform the matched substring, as seems to be indicated by your line
m.group().replaceAll("something","");
the best solution is probably to use
A StringBuffer for the result
Matcher.appendReplacement and Matcher.appendTail.
Example:
String regex = "ipsum";
String sourceString = "lorem ipsum dolor sit";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sourceString);
StringBuffer sb = new StringBuffer();
while (m.find()) {
// For example: transform match to upper case
String replacement = m.group().toUpperCase();
m.appendReplacement(sb, replacement);
}
m.appendTail(sb);
sourceString = sb.toString();
System.out.println(sourceString); // "lorem IPSUM dolor sit"
Assuming you want to replace all occurences of a certain pattern, try this:
String source = "aabbaabbaabbaa";
String result = source.replaceAll("aa", "xx"); //results in xxbbxxbbxxbbxx
Removing the pattern would then be:
String result = source.replaceAll("aa", ""); //results in bbbbbb