Regex pattern java with commas - java

I have a below string which comes from an excel column
"\"USE CODE \"\"Gef, sdf\"\" FROM 1/7/07\""
I would like to set regex pattern to retrieve the entire string,so that my result would be exactly like
"USE CODE ""Gef, sdf"" FROM 1/7/07"
Below is what I tried
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
public static void main( String args[] ){
// String to be scanned to find the pattern.
String line = "\"USE CODE \"\"Gef, sdf\"\" FROM 1/7/07\", Delete , Hello , How are you ? , ";
String line2 = "Test asda ds asd, tesat2 . test3";
String dpattern = "(\"[^\"]*\")(?:,(\"[^\"]*\"))*,|([^,]+),";
// Create a Pattern object
Pattern d = Pattern.compile(dpattern);
Matcher md = d.matcher(line2);
Pattern r = Pattern.compile(dpattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: 0 " + m.group(0) );
// System.out.println("Found value: 1 " + m.group(1) );
//System.out.println("Found value: 2 " + m.group(2) );
} else {
System.out.println("NO MATCH");
}
}
}
and the result out of it breaks after ,(comma) and hence the output is
Found value: 0 "USE CODE ""Gef,
It should be
Found value: 0 "USE CODE ""Gef sdf"" FROM 1/7/07",
and for the second line Matcher m = r.matcher(line2); the output should be
Found value: 0 "Test asda ds asd",

You may use
(?:"[^"]*(?:""[^"]*)*"|[^,])+
See the regex demo
Explanation:
" - leading quote
[^"]* - 0+ chars other than a double quote
(?:""[^"]*)* - 0+ sequences of a "" text followed with 0+ chars other than a double quote
" - trailing quote
OR:
[^,] - any char but a comma
And the whole pattern is matched 1 or more times as it is enclosed with (?:...)+ and + matches 1 or more occurrences.
IDEONE demo:
String line = "\"USE CODE \"\"Gef, sdf\"\" FROM 1/7/07\", Delete , Hello , How are you ? , ";
String line2 = "Test asda ds asd, tesat2 . test3";
Pattern pattern = Pattern.compile("(?:\"[^\"]*(?:\"\"[^\"]*)*\"|[^,])+");
Matcher matcher = pattern.matcher(line);
if (matcher.find()){ // if is used to get the 1st match only
System.out.println(matcher.group(0));
}
Matcher matcher2 = pattern.matcher(line2);
if (matcher2.find()){
System.out.println(matcher2.group(0));
}

Related

Trying to split a string into 3 parts with Regex

I need to split up a JSONPath path into 3 parts if it has a separator. A separator would be an indicator of an array.
For example:
$.Colors[*].name
Would need to become:
Before: "$.Colors"
Separator: "[*]"
After: ".name"
In the event that there's multiple:
like:
$.Colors[*].Color[*].name
It would need to take the first:
Before: "$.Colors"
Separator: "[*]"
After: ".Color[*].name"
I also want this to work on filters:
$.Colors[?(#.type == 'Primary')].Color[*].name
It would split on that filter value.
Before: "$.Colors"
Separator: "[?(#.type == 'Primary')]"
After: ".Color[*].name"
My attempts have been fruitless thus far:
static private String regexString = "\\[\\*]|\\[\\?\\(.*\\)]";
static private Pattern pattern = Pattern.compile(regexString);
private boolean splittable;
private String pre;
private String post;
private String split;
PathSplitter(String path) {
Matcher matcher = pattern.matcher(path);
if (!matcher.find()) {
splittable = false;
}
else {
splittable = true;
split = matcher.group(0);
//pre = matcher.group(1);
//post = matcher.group(2);
}
}
Any help would be great!
The regex you need is this for getting the expected matches as mentioned in your post,
(.*?)(\[[^[\]]*\])(.*)
Here,
(.*?) - This part captures the Before part anything as less as possible before the separator pattern and captures the data in group1
(\[[^[\]]*\]) - This part captures the separator which starts with literal [ followed by any character other than [ and ] zero or more followed by a closing ]
(.*) - Finally this captures the remaining of After part
Regex Demo
Java code,
List<String> list = Arrays.asList("$.Colors[*].name","$.Colors[*].Color[*].name","$.Colors[?(#.type == 'Primary')].Color[*].name");
Pattern p = Pattern.compile("(.*?)(\\[[^\\[\\]]*\\])(.*)");
list.forEach(x -> {
Matcher m = p.matcher(x);
if (m.matches()) {
System.out.println("For string: " + x);
System.out.println("Before: "+ m.group(1));
System.out.println("Separator: "+ m.group(2));
System.out.println("After: "+ m.group(3));
System.out.println();
}
});
Prints the following like you expected,
For string: $.Colors[*].name
Before: $.Colors
Separator: [*]
After: .name
For string: $.Colors[*].Color[*].name
Before: $.Colors
Separator: [*]
After: .Color[*].name
For string: $.Colors[?(#.type == 'Primary')].Color[*].name
Before: $.Colors
Separator: [?(#.type == 'Primary')]
After: .Color[*].name

Java Regex OR operator not working properly

I have this Strings :
String test1=":test:block1:%a1%a2%a3%a4:block2:BL";
and
String test2=":test:block2:BL:block1:%a1%a2%a3%a4";
I've created an regex pattern in order to isolate this piece of String
block1:%a1%a2%a3%a4:
from the rest of the String letting those Strings like this :
in the case of test1="block1:%a1%a2%a3%a4:"; (with ':' at the end)
in the case of test2=":block1:%a1%a2%a3%a4"; (with ':' at the beggining)
The regex i've created is :
"(block1:(.*?):|:block1:(.*))";
With test1 is working , but with test2 is retrieving me this :
block1:%a1%a2%a3%a4:block2:BL";
Can someone give me a hand with this ?
Cheers!
You may use
block1:([^:]*)
It matches block1: text and then captures into Group 1 any 0 or more chars other than :.
See Java demo:
String patternString = "block1:([^:]*)";
String[] tests = {":test:block1:%a1%a2%a3%a4:block2:BL",
":test:block2:BL:block1:%a1%a2%a3%a4"};
for (int i=0; i<tests.length; i++)
{
Pattern p = Pattern.compile(patternString, Pattern.DOTALL);
Matcher m = p.matcher(tests[i]);
if(m.find())
{
System.out.println(tests[i] + " matched. Match: " +
m.group(0) + ", Group 1: " + m.group(1));
}
}
Output:
:test:block1:%a1%a2%a3%a4:block2:BL matched. Match: block1:%a1%a2%a3%a4, Group 1: %a1%a2%a3%a4
:test:block2:BL:block1:%a1%a2%a3%a4 matched. Match: block1:%a1%a2%a3%a4, Group 1: %a1%a2%a3%a4

Java Regex jumps to next match with if clause [duplicate]

I would like to be able to find the first occurrence of m² and then numbers in front of it, could be integers or decimal numbers.
E.g.
"some text" 38 m² "some text" ,
"some text" 48,8 m² "some text",
"some text" 48 m² "some text", etc..
What I have so far is:
\d\d,\d\s*(\m\u00B2)|\d\d\s*(\m\u00B2)
This right now finds all occurrences, although I guess it could be fixed with findFirst(). Any ideas how to improve the Regex part?
To get the first match, you just need to use Matcher#find() inside an if block:
String rx = "\\d+(?:,\\d+)?\\s*m\\u00B2";
Pattern p = Pattern.compile(rx);
Matcher matcher = p.matcher("E.g. : 4668,68 m² some text, some text 48 m² etc");
if (matcher.find()){
System.out.println(matcher.group());
}
See IDEONE demo
Note that you can get rid of the alternation group using an optional non-capturing group (?:..)?
Pattern breakdown:
\d+ - 1+ digits
(?:,\d+)? - 0+ sequences of a comma followed with 1+ digits
\s* - 0+ whitespace symbols
m\u00B2 - m2.
This is what I came up with you help :) (work in progress, later it should return BigDecimal value), for now it seems to work:
public static String findArea(String description) {
String tempString = "";
Pattern p = Pattern.compile("\\d+(?:,\\d+)?\\s*m\\u00B2");
Matcher m = p.matcher(description);
if(m.find()) {
tempString = m.group();
}
//remove the m and /u00B2 to parse it to BigDecimal later
tempString = tempString.replaceAll("[^0-9|,]","");
System.out.println(tempString);
return tempString;
}
One simple way of doing it!
description.replaceFirst(#NotNull String regex,
#NotNull String replacement)
JAVADoc: Replaces the first substring of this string that matches the given regular expression with the given replacement.
To find only last one:
#Test
public void testFindFirstRegExp() {
String pattern = ".* (\\d+,\\d+) .*";
Pattern r = Pattern.compile(pattern);
String line = "some text 44,66 m² some 33,11 m² text 11,22 m² some text";
Matcher m = r.matcher(new StringBuilder(line).reverse().toString());
String expected = "44,66";
String actual = null;
if (m.find()) {
actual = new StringBuilder(m.group(1)).reverse().toString();
}
System.out.println("got first:" + actual);
Assert.assertEquals(expected, actual);
m = r.matcher(line);
expected = "11,22";
actual = null;
if (m.find()) {
actual = m.group(1);
}
System.out.println("got last:" + actual);
Assert.assertEquals(expected, actual);
}
prints:
got first:44,66
got last:11,22
Note: think that you need to reverse pattern when needed for ex:
pattern = ".* (\\d+,\\d+-?) .*"; //reverse for (-?\\d+,\\d+)
but this will work as waited:
pattern = " (\\-?\\d+,\\d+) ";
you get all of them in loop:
while (m.find()) {
actual = m.group(1);
System.out.println("got last:" + actual);
}
Will print:
got last:44,66
got last:33,11
got last:11,22

Java how to setup regex for this string

So I'm trying to pull two strings via a matcher object from one string that is stored in my online databases.
Each string appears after s:64: and is in quotations
Example s:64:"stringhere"
I'm currently trying to get them as so but any regex that I've tried has failed,
Pattern p = Pattern.compile("I don't know what to put as the regex");
Matcher m = p.matcher(data);
So with that said, all I need is the regex that will return the two strings in the matcher so that m.group(1) is my first string and m.group(2) is my second string.
Try this regex:-
s:64:\"(.*?)\"
Code:
Pattern pattern = Pattern.compile("s:64:\"(.*?)\"");
Matcher matcher = pattern.matcher(YourStringVar);
// Check all occurance
int count = 0;
while (matcher.find() && count++ < 2) {
System.out.println("Group : " + matcher.group(1));
}
Here group(1) returns the each match.
OUTPUT:
Group : First Match
Group : Second Match
Refer LIVE DEMO
String data = "s:64:\"first string\" random stuff here s:64:\"second string\"";
Pattern p = Pattern.compile("s:64:\"([^\"]*)\".*s:64:\"([^\"]*)\"");
Matcher m = p.matcher(data);
if (m.find()) {
System.out.println("First string: '" + m.group(1) + "'");
System.out.println("Second string: '" + m.group(2) + "'");
}
prints:
First string: 'first string'
Second string: 'second string'
Regex you need should be compile("s:64:\"(.*?)\".*s:64:\"(.*?)\"")

java regex string matches and multiline delimited with new line

How do I write a regex that will match multiline delmitied by new line and spaces?
The following code works for one multiline but does not work if the input
is
String input = "A1234567890\nAAAAA\nwwwwwwww"
By which I mean matches() is not true for the input.
Here is my code:
package patternreg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pattrenmatching {
public static void main(String[] args) {
String input = "A1234567890\nAAAAA";
String regex = ".*[\\w\\s\\w+].*";
Pattern p = Pattern.compile(regex,Pattern.MULTILINE);
Matcher m =p.matcher(input);
if (m.matches()) {
System.out.println("matches() found the pattern \""
+ "\" starting at index "
+ " and ending at index ");
} else {
System.out.println("matches() found nothing");
}
}
}
You could also add the DOTALL flag to get it working:
Pattern p = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
I believe your problem is that .* is greedy, so it's matching all the other '\n' in the string.
If you want to stick with the code above try: "[\S]*[\s]+". Which means match zero or more non-whitespace chars followed by one or more whitespace chars.
fixed up code:
public static void main(String[] args) {
String input = "A1234567890\nAAAAA\nsdfasdf\nasdfasdf";
String regex = "[\\S]*[\\s]+";
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(input.substring(m.start(), m.end()) + "*");
}
if (m.matches()) {
System.out.println("matches() found the pattern \"" + "\" starting at index " + " and ending at index ");
} else {
System.out.println("matches() found nothing");
}
}
OUTPUT:
A1234567890
* AAAAA
* sdfasdf
* matches() found nothing
Also, a pattern of
"([\\S]*[\\s]+)+([\\S])*"
will match the entire output (matcher returns true) but messes up the token part of your code.

Categories