Find all occurrences of a regex pattern in a line - java

My String is like this (one single line):
String input = "Details of all persons. Person=details=John Smith-age-22; Person=details=Alice Kohl-age-23; Person=details=Ram Mohan-city-Dallas; Person=details=Michael Jack-city-Boston;"
I want to find out using regex matching all the persons with its details (basically text from details upto the char prior to semicolon). I am interested in finding:
details=John Smith-age-22
details=Alice Kohl-age-23
details=Ram Mohan-city-Dallas
details=Michael Jack-city-Boston
Can someone tell me how to do this ? Sorry, I could not find any example like that over the net. Thanks.

You can try this code.
public static void main(String[] args) {
String input = "Details of all persons. Person=details=John Smith-age-22; Person=details=Alice Kohl-age-23; Person=details=Ram Mohan-city-Dallas; Person=details=Michael Jack-city-Boston;";
Pattern pattern = Pattern.compile("(?<=Person=).*?(?=;)");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String str = matcher.group();
System.out.println(str);
}
}
No assertion
public static void main(String[] args) {
String input = "Details of all persons. Person=details=John Smith-age-22; Person=details=Alice Kohl-age-23; Person=details=Ram Mohan-city-Dallas; Person=details=Michael Jack-city-Boston;";
Pattern pattern = Pattern.compile("Person=.*?;");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String str = matcher.group();
System.out.println(str.substring(7, str.length()-1));
}
}

I suspect you will find it easiest if you put the fields you are looking for into groups so that you can extract the details you want.
Something like:
Pattern personPattern = Pattern.compile("Person=details=(\\w+)-(age-\\d+|city-\\w+); ");
Matcher matcher = personPattern.match(input);
while (matcher.find()) {
String name = matcher.group(1);
String field = matcher.group(2);
...
}

Related

How to get patterns of groups in Java regex?

Assume that I am given a regex pattern, whole_pattern
Pattern p = Pattern.compile(whole_pattern);
Matcher m = p.matcher(line);
if (m.find()) {
String s1 = m.group(1);
String s2 = m.group(2);
}
Obviously, we can get matched string of each group. But can we get the pattern of each group in the whole_pattern string? For example if whole_pattern = (\\d+)(\\w+), then patterns of group 1 and group 2 are \\d+ and \\w+, respectively.
You can use regexp for regexp:
public void simpleTest() {
String whole_pattern = "(\\d+)(\\w+)";
System.out.println(patternGroups(whole_pattern));
}
private List<String> patternGroups(String patternString) {
List<String> result = new ArrayList<>();
String pattern = "\\(([^()]+)\\)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(patternString);
while (m.find()) {
result.add(m.group(1));
}
return result;
}
Output of simpleTest() call will be:
[\d+, \w+]
Split your pattern string by ) and remove the first character in each elementh of the resulting array.

How to preserve delimeters while using String.split() in Java?

String TextValue = "hello{MyVar} Discover {MyVar2} {MyVar3}";
String[] splitString = TextValue.split("\\{*\\}");
What I'm getting output is [{MyVar, {MyVar2, {MyVar3] in splitString
But my requirement is to preserve those delimiters {} i.e. [{MyVar}, {MyVar2}, {MyVar3}].
Required a way to match above output.
Use something like so:
Pattern p = Pattern.compile("(\\{\\w+\\})");
String str = ...
Matcher m = p.matcher(str);
while(m.find())
System.out.println(m.group(1));
Note, the code above is untested but that will look for words within curly brackets and place them in a group. It will then go over the string and output any string which matches the expression above.
An example of the regular expression is available here.
Thanks kelvin & npinti.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class CreateMatcherExample {
public static void main(String[] args) {
String TextValue = "hello{MyVar} Discover {My_Var2} {My_Var3}";
String patternString = "\\{\\w+\\}";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(TextValue);
while(matcher.find()) {
System.out.println(matcher.group());
}
}
}

Get a particular string from a data using regular expression

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

Unable to Parse reqular expression in JAVA

This is my code,
String xyz = "{\"status\":\"ok\",\"data\":[{\"RatingCount\":4}], [{\"RatingCount\":1}], [{\"RatingCount\":1}]\"code\":1}";
String pattern = ".*],\\s*\\[.*";//"(.*)(],\\s.*\\[)(.*)";
Pattern p1 = Pattern.compile(pattern);
Matcher m1 = p1.matcher(xyz);
boolean b = m1.matches();
System.out.println(b);
I would like to replace pattern '], [' with "".
I used replaceAll but no luck
Check it in working Fiddle
Use the regex as below
String pattern = "\],[ ]*\[";
xyz =xyz.replaceAll("]\s*,\s*\[", "],[");
This worked like charm and also this link helped me alot http://regexr.com/
Thank you for all your inputs!
The output of this block is: {"status":"ok","data":[{"RatingCount":4}{"RatingCount":1}{"RatingCount":1}]"code":1}
public static void main(String[] args) {
String str = "{\"status\":\"ok\",\"data\":[{\"RatingCount\":4}], [{\"RatingCount\":1}], [{\"RatingCount\":1}]\"code\":1}";
System.out.println(replace(str, "\\],\\s+\\["));
}
public static String replace(String str, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll("");
}

how to find "B" + max 6 signs

I need to validate, if pattern has letter "B" and after it up to six signs (letters and digits). For example: we have abcdB1234B123456. Found answers should be: B1234 and B123456.
I made this pattern:
[^B]{1,6}
but its not precise..
What about this pattern:
public static void main(String[] args) {
final Pattern pattern = Pattern.compile("B[aAc-zC-Z0-9]{0,6}");
final String string = " abcdB1234B123456";
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
Output:
B1234
B123456
Try this code :
String data = "abcdB1234B123456";
Pattern pattern = Pattern.compile("B[aAc-zC-Z\\d]{0,6}");
Matcher matcher = pattern.matcher(data);
while (matcher.find()) {
// Indicates match is found. Do further processing
System.out.println(matcher.group());
}

Categories

Resources