from this -> contractor:"Hi, this is \"Paul\", how are you?" client:"Hi ...." <-
I want to get just -> Hi, this is \"Paul\", how are you? <-
I need a regular expression in java to do that I try it but I m struggle with the inner quotation (\") is driving me mad.
Thanks for any hint.
Java supports lookbehinds, so vanilla regex:
"(.*?(?<!\\))"
Inside a Java string (see https://stackoverflow.com/a/37329801/1225328):
\"(.*?(?<!\\\\))\"
The actual text will be contained inside the first group of each match.
Demo: https://regex101.com/r/8OXujX/2
For example, in Java:
String regex = "\"(.*?(?<!\\\\))\"";
String input = "contractor:\"Hi, this is \\\"Paul\\\", how are you?\" client:\"Hi ....\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) { // or while (matcher.find()) to iterate through all the matches
System.out.println(matcher.group(1));
} else {
System.out.println("No matches");
}
Prints:
Hi, this is \"Paul\", how are you?
The regexp should be like this: "(?:\\.|[^"\\])*"
Online demo
It uses non-capturing group ?:, matching any character . or a single character NOT in the list of double quote and backslash.
var text1 = "contractor:\"Hi, this is \\\"Paul\\\", how are you?\" client:\"Hi ....\" <-";
var regExWithQuotation = "contractor:(.+\".+\".+) client:";
Pattern p = Pattern.compile(regExWithQuotation);
var m = p.matcher(text1);
;
if (m.find()) {
var res = m.group(1);
System.out.println(res);
}
var regExWithoutQuotation = "contractor:\"(.+\".+\".+)?\" client:";
p = Pattern.compile(regExWithoutQuotation);
m = p.matcher(text1);
if (m.find()) {
var res = m.group(1);
System.out.println(res);
}
Output is:
"Hi, this is "Paul", how are you?"
Hi, this is "Paul", how are you?
You can use the regex, (?<=contractor:\").*(?=\" client:)
Description of the regex:
(?<=contractor:\") specifies positive lookbehind for contractor:\"
.* specifies any character
(?=\" client:) specifies positive lookahead for \" client:
In short, anything preceded by contractor:\" and followed by \" client:
Demo:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "contractor:\"Hi, this is \\\"Paul\\\", how are you?\" client:\"Hi ....\"";
String regex = "(?<=contractor:\").*(?=\" client:)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Output:
Hi, this is \"Paul\", how are you?
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
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 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
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());
}