Getting specific words from a sentence - java

I have a sentence which looks like this {Name=Zeus, Address=Something 21} . I want to get only the words after the = (Zeus Something 21), but it's not working for the both of them. That's my code
String line="";
Pattern pattern = Pattern.compile("[=]+([A-Za-z0-9-_]+)");
for (Entity entity : pq.asIterable()) {
String placeInfo=entity.getProperties().toString();
line=placeInfo;
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
result.add(matcher.group());
}
and that's the result i get: =Zeus.
I suppose it's pattern's fault but i don't know what to change... Any ideas?
The placeinfo equals to something that looks like {Name=Something, Address=Something 21} and it's entity properties i'm getting from my datastore.

Your regex and code seem very close to expected result;
String line = "{Name=Zeus, Address=Something 21}";
Pattern pattern = Pattern.compile("[=]+([A-Za-z0-9-_ ]+)"); //added space
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
System.out.println(matcher.group(1)); //did you try group(1)
}

I'm not sure this is the correct approach? What is the return type of entity.getProperties()? If it's a Map or a Properties or a JSON object you are probably best to use an appropriate getter rather than using a regex on the toString().
eg
Map<String, String> properties = entity.getProperties();
String name = properties.get("Name");
String address = properties.get("Address");

Pattern pattern = Pattern.compile("{Name=(.+), Address=(.+)}");
Matcher matcher = pattern.matcher("{Name=Zeus, Address=Something 21}");
if (!matcher.matches()) throw new RuntimeException();
String name = matcher.group(1);
String address = matcher.group(2);

Related

I Want to extract a certain number from string in java but not able to do so

I have a string like String a = "I have 102 string but 123453 is best"
So, In the above string i want to extract only 123453
This string keep getting changes but i always want to get that second number from this string.
What is the best possible way to do this?
Here is a complete example. First define your pattern:
Pattern p = Pattern.compile("[0-9]+");
Then define the string you want to test
String test = "I have 102 string but 123453 is best";
Create a matcher for that string
Matcher matcher = p.matcher(test);
Find once
matcher.find(); // find once
Find again, and if found, get the string
if (matcher.find()) { // find twice
System.out.println(matcher.group());
}
For a complete, minimal and repeatable example, consider defining a method and defining the pattern as a constant:
static final Pattern P = Pattern.compile("[0-9]+");
static String findSecondNumber(String test) {
Matcher matcher = P.matcher(test);
matcher.find(); // find once
if (matcher.find()) { // find twice
return matcher.group();
}
return null; // or alternatively return an empty string or a default value
}
Where matcher.group() returns the full match found; see also the documentation here.
It can be done in a single find() with capturing groups. Here is how:
String a = "I have 102 string but 123453 is best"
Pattern p = Pattern.compile("^.*\d+.*(\d*).*$");
Matcher m = p.matcher(a);
if (m.find()) {
System.out.println(m.group(1)); // this will print 123453
}

How to Split with particular condition in Java?

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());
}

regular expression to match a string in order

I have string as follows
"ValueFilter("val1") AND ColumnFilter("val2") AND ValueFilter("val3")"
I have stored the following regex in a array. Using for loop I tried to match the pattern
"ValueFilter\\((.*?)\\)","ColumnFilter\\((.*?)\\)"
what I will do is I will replace the value in the bracket and copy it to a new string.
When I run this above regex against the string in the first loop i have XFilter so it will match both occurrence. But I want to do this in order.
Here is the i thing i want to achieve
first i want to match ValueFilter first then ColumnFilter then again ValueFilter. How can I achieve this?
Edit : Added Code
String expr = "\"ValueFilter(\"val1\") AND ColumnFilter(\"val2\") AND ValueFilter(\"val3\")\"";
String patterns = {"ValueFilter\\((.*?)\\)", "ColumnFilter\\((.*?)\\)"}
for (String pattern : patterns) {
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(expr);
while (m.find()) {
//do something
}
}
Expected Output
ValueFilter("val1")
ColumnFilter("val2")
ValueFilter("val3")
You can use this regex [XY]Filter\((.*?)\) with pattern and you have to loop throw the matches using :
String str = "\"XFilter(\"val1\") AND YFilter(\"val2\") AND XFilter(\"val3\")\"";
String regex = "[XY]Filter\\((.*?)\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
Note you can i use [XY] which mean to match both X or Y,
Output
XFilter("val1")
YFilter("val2")
XFilter("val3")
regex demo
If you want to get only the value you can get the group 1 like matcher.group(1) instead, the output should be :
"val1"
"val2"
"val3"
Edit
what if I have filtername as "ValueFilter" and "ColumnFilter" instead
of X and Y
In this case you can use (Value|Column) instead of [XY] which mean match ValueFilter or ColumnFilter, the regex should look like :
String str = "\"ValueFilter(\"val1\") AND ColumnFilter(\"val2\") AND ValueFilter(\"val3\")\"";
String regex = "(Value|Column)Filter\\((.*?)\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
Output
ValueFilter("val1")
ColumnFilter("val2")
ValueFilter("val3")
Check code demo

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

First and second tocen regex

How could I get the first and the second text in "" from the string?
I could do it with indexOf but this is really boring ((
For example I have a String for parse like: "aaa":"bbbbb"perhapsSomeOtherText
And I d like to get aaa and bbbbb with the help of Regex pattern - this will help me to use it in switch statement and will greatly simplify my app/
If all that you have is colon delimited string just split it:
String str = ...; // colon delimited
String[] parts = str.split(":");
Note, that split() receives regex and compilies it every time. To improve performance of your code you can use Pattern as following:
private static Pattern pColonSplitter = Pattern.compile(":");
// now somewhere in your code:
String[] parts = pColonSplitter.split(str);
If however you want to use pattern for matching and extraction of string fragments in more complicated cases, do it like following:
Pattert p = Patter.compile("(\\w+):(\\w+):");
Matcher m = p.matcher(str);
if (m.find()) {
String a = m.group(1);
String b = m.group(2);
}
Pay attention on brackets that define captured group.
Something like this?
Pattern pattern = Pattern.compile("\"([^\"]*)\"");
Matcher matcher = pattern.matcher("\"aaa\":\"bbbbb\"perhapsSomeOtherText");
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Output
aaa
bbbbb
String str = "\"aaa\":\"bbbbb\"perhapsSomeOtherText";
Pattern p = Pattern.compile("\"\\w+\""); // word between ""
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group().replace("\"", ""));
}
output:
aaa
bbbbb
there are several ways to do this
Use StringTokenizer or Scanner with UseDelimiter method

Categories

Resources