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
Related
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());
}
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.
I have random String for example "aaaaaaBccccCCCCd" I need make regex which searches the text for groups to get effect "a6B1c4C4d1". My regex looks like that "(\\D+)\\D*\\1" but he lost single letters, so in this sample B and d.
Maybe someone would have an idea?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Compress {
public static void main(String[] args) {
String text = "aaaaaaBccccCCCCd";
String regex = "(\\D+)\\D*\\1"; // or (.+).*\\1
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
String result = new String();
while (matcher.find()) {
String letter = matcher.group().substring(0, 1);
String numberOfLetter = String.valueOf(matcher.group().length());
result = result + letter + numberOfLetter;
}
System.out.println(result);
}
}
Thank you.
Use the following approach based on Matcher#appendReplacement:
String text = "aaaaaaBccccCCCCd"; //a6B1c4C4d1
String regex = "(.)(\\1*)";
String pattern = "test";
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(text);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group(1) + (m.group(2).length()+1));
}
m.appendTail(sb);
System.out.println(sb);
See the Java demo
The (.)(\1*) will capture any char into Group 1 and then will capture into Group 2 zero or more repetitions of the same content. In the "callback", Group 1 is concatenated with the length of Group 2 incremented to account for the Group 1 length.
I have a string like this
"abc:def"
I need to check if it matches. If yes, I want to apply a regex and retrieve first part of it ("abc").
Here is my try:
Pattern pattern = Pattern.compile(".*:.*");
String name = "abc:def"
Matcher matcher = pattern.matcher(name);
if (matcher.find()) {
String group = matcher.group(1);
System.out.println(group);
}
It gives me
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1
You need to add a capturing group inside your regular expression. This is done by putting what you want to capture inside parentheses:
Pattern pattern = Pattern.compile("(.*):.*"); // <-- parentheses here
String name = "abc:def";
Matcher matcher = pattern.matcher(name);
if (matcher.find()) {
String group = matcher.group(1);
System.out.println(group); // prints "abc"
}
You only have a group (0) (full match) because you don't define any capturing groups (enclosed in ( and )) in your regex.
If you change your code to include a capturing group like this
Pattern pattern = Pattern.compile("([^:]*):.*");
String name = "abc:def";
Matcher matcher = pattern.matcher(name);
if (matcher.find()) {
String fullMatch = matcher.group(0);
String group = matcher.group(1);
System.out.println(group);
}
you'll have full match "abc:def" and first group (until first colon) "abc"
There is no capturing group in your Pattern:
Pattern pattern = Pattern.compile(".*:.*");
See the documentation of Pattern for more details.
I assume you need this instead:
Pattern pattern = Pattern.compile("(.*):.*");
Try this:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class HelloWorld
{
public static void main(String[] args)
{
String patternString = ".*:.*";
Pattern pattern = Pattern.compile(patternString);
String name = "abc:def";
Matcher matcher = pattern.matcher(name);
while(matcher.find()) {
System.out.println("found: " + matcher.group(0));
}
}
}
I have a long string let's say
I like this #computer and I want to buy it from #XXXMall.
I know the regular expression pattern is
Pattern tagMatcher = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");
Now i want to get all the hashtags in an array. How can i use this expression to get array of all hash tags from string something like
ArrayList hashtags = getArray(pattern, str)
You can write like?
private static List<String> getArray(Pattern tagMatcher, String str) {
Matcher m = tagMatcher.matcher(str);
List<String> l = new ArrayList<String>();
while(m.find()) {
String s = m.group(); //will give you "#computer"
s = s.substring(1); // will give you just "computer"
l.add(s);
}
return l;
}
Also you can use \\w- instead of A-Za-z0-9-_ making the regex [#]+[\\w]+\\b
This link would surely be helpful for achieving what you want.
It says:
The find() method searches for occurrences of the regular expressions
in the text passed to the Pattern.matcher(text) method, when the
Matcher was created. If multiple matches can be found in the text, the
find() method will find the first, and then for each subsequent call
to find() it will move to the next match.
The methods start() and end() will give the indexes into the text
where the found match starts and ends.
Example:
String text =
"This is the text which is to be searched " +
"for occurrences of the word 'is'.";
String patternString = "is";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
int count = 0;
while(matcher.find()) {
count++;
System.out.println("found: " + count + " : "
+ matcher.start() + " - " + matcher.end());
}
You got the hint now.
Here is one way, using Matcher
Pattern tagMatcher = Pattern.compile("#+[-\\w]+\\b");
Matcher m = tagMatcher.matcher(stringToMatch);
ArrayList<String> hashtags = new ArrayList<>();
while (m.find()) {
hashtags.add(m.group());
}
I took the liberty of simplifying your regex. # does not need to be in a character class. [A-Za-z0-9_] is the same as \w, so [A-Za-z0-9-_] is the same as [-\w]
You can use :
String val="I like this #computer and I want to buy it from #XXXMall.";
String REGEX = "(?<=#)[A-Za-z0-9-_]+";
List<String> list = new ArrayList<String>();
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(val);
while(matcher.find()){
list.add(matcher.group());
}
(?<=#) Positive Lookbehind - Assert that the character # literally be matched.
you can use the following code for getting the names
String saa = "#{akka}nikhil#{kumar}aaaaa";
Pattern regex = Pattern.compile("#\\{(.*?)\\}");
Matcher m = regex.matcher(saa);
while(m.find()) {
String s = m.group(1);
System.out.println(s);
}
It will print
akka
kumar