Java Regular Expression using slash - java

My String is "T1/T2\/T3/T4\\/T5\\"
My Expected output is "T1","T2/T3","T4\", "T5\"(split using /). if / is prefixed by \ then that / is treated as string character not a separator.
I am following the below code, `
String pattern = "((?!/)(\\/|[^/])+(?=/)?)+";
Pattern r = Pattern.compile(pattern);
String path = {getting from request}; //Raw type T1/T2\/T3/T4\\/T5\\
Matcher m = r.matcher(path);
while (m.find()) {
System.out.println(m.group(0));
}`
my result is "T1/T2/T3/T4\/T5\" it is not an expected output.
How I can change my regular expression so that it will give expected result?
in backend my string will change since I am sending ?

If you pass a literal string T1/T2\/T3/T4 as input you can use
String path = "T1/T2\\/T3/T4"; // <-- Note how the string must be coded in Java
String pattern = "(?s)(?:\\\\.|[^\\\\/])+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(path);
while (m.find()) {
System.out.println(m.group(0));
}
// => T1
// T2\/T3
// T4
See the Java demo and the regex demo.
Details:
(?s) - a Pattern.DOTALL flag option
(?:\\.|[^\\/])+ - a non-capturing group, one or more occurrences that matches either of the two patterns:
\\. - any single char escaped with a backslash
[^\\/] - any char other than / and a backslash.

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex
{
public static void main(String[] args)
{
String pattern = "((?!/)(\\\\/|[^/])+(?=/)?)+";
Pattern r = Pattern.compile(pattern);
String path = args[0];
Matcher m = r.matcher(path);
while (m.find()) {
System.out.println(m.group(0));
}
}
}

Related

Regular expression in java (java String)

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?

Regex designing for special string

Unable to implement regex for string in line starts with ? and ends with ;
appended with single quotes or double quotes.
for example:
?abcdef;
'?abcdef;'
"?abcdef;"
I tried a lot, like this "^\\?([^;]+)\\;$" but it did not work.
Test code snippet:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
public static void main(final String[] args) {
final String text = "This is param-start ?abcdef; param-end";
final String patternString = "(['\"]?)\\?.*;\\1";
final Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(text);
System.out.println("lookingAt = " + matcher.lookingAt());
System.out.println("matches = " + matcher.matches());
}
}
You may use this regex:
(["']?)\?.*;\1
For Java use:
final String pattern = "(['\"]?)\\?.*;\\1";
RegEx Demo
(["']?) matches an optional ' or " and captures in group #1
\1 is back-reference for the same value as in group #1
You may use
final String text = "This is param-start ?abcdef; param-end";
final String patternString = "(['\"]?)\\?.*?;\\1";
final Pattern pattern = Pattern.compile(patternString, Pattern.DOTALL);
final Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println("Match found: " + matcher.group());
}
// => Match found: = ?abcdef;
See the Java demo and the regex demo. Regulex graph:
Notes:
while (matcher.find()) - iterates through all potential matches in the string
matcher.group() - accesses the match value.
Regex
(['"]?) - Capturing group 1: either ' or "
\? - a ? char
.*? - any 0+ chars, as few as possible (the pattern is compiled with Pattern.DOTALL, so it will match across line breaks, too)
; - a semi-colon
\1 - the same value as captured in Group 1.
Your description stated "starts with ? and ends with ;", but your sample string does not end with a semicolon - it's ending with wtih a double quote.
If you want to allow the string to start or end with either a single quote or double quote, your match should be:
^['"]?\?[^;]+;\['"]?$

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

Got Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1

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

Categories

Resources