I have the following program,
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String VALID_GUID_REGEX = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}";
Pattern NOT_PREFIXED_FILES_REGEX =
Pattern.compile("(^"+VALID_GUID_REGEX+"/\\b(foo|bar)\\b.*)|^[^/]+$");
List<String> list = new ArrayList<>();
list.add("256a5037-9fc1-4e60-95c3-523d5ae1c935/foo/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a");
list.add("256a5037-9fc1-4e60-95c3-523d5ae1c935/bar/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a");
list.add("govcorp/123a5037-9fc1-4e60-95c3-523d5ae1c935/foo/text.doc");
list.add("156a5037-9fc1-4e60-95c3-523d5ae1c935/123a5037-9fc1-4e60-95c3-523d5ae1c935/delta/text.doc");
list.add("123a5037-9fc1-4e60-95c3-523d5ae1c935/");
String[] keys = list.stream()
.filter(k -> NOT_PREFIXED_FILES_REGEX.matcher(k).find())
.toArray(String[]::new);
System.out.println(Arrays.toString(keys));
}
}
And the code works fine except the last item in list, i need the following condition to be satisfied,
256a5037-9fc1-4e60-95c3-523d5ae1c935/foo/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a -- Pass
256a5037-9fc1-4e60-95c3-523d5ae1c935/bar/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a -- Pass
govcorp/123a5037-9fc1-4e60-95c3-523d5ae1c935/foo/text.doc -- Fail
156a5037-9fc1-4e60-95c3-523d5ae1c935/123a5037-9fc1-4e60-95c3-523d5ae1c935/foo/text.doc -- Fail
123a5037-9fc1-4e60-95c3-523d5ae1c935/ - Pass
Let's consider first line,
256a5037-9fc1-4e60-95c3-523d5ae1c935/bar/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a
If i give my input "256a5037-9fc1-4e60-95c3-523d5ae1c935/" - Pass and "256a5037-9fc1-4e60-95c3-523d5ae1c935/bar/" - Pass, am getting file path from server.
Let's consider fail case, "govcorp/" - Fail and "govcorp/123a5037-9fc1-4e60-95c3-523d5ae1c935/" - Fail
If two GUID sequence case should FAIL, such as
156a5037-9fc1-4e60-95c3-523d5ae1c935/123a5037-9fc1-4e60-95c3-523d5ae1c935/ - FAIL
If only one GUID case such as "123e4567-e89b-12d3-a456-426655440001/" - Pass
Here, we would first fail our undesired strings with a simple expression:
^((?!\.doc).)*$
Demo 1
then for the remaining strings, we would be designing a second expression, which in this case, your original expression works just fine, and we might just want to wrap that with a capturing group:
([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})
Demo 2
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})";
final String string = "256a5037-9fc1-4e60-95c3-523d5ae1c935/foo/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a\n"
+ "256a5037-9fc1-4e60-95c3-523d5ae1c935/bar/44434038019,2019-05-24T09:02:18.695Z,b4786bf4-157a-4f1b-a030-4c5416e1884a\n"
+ "123a5037-9fc1-4e60-95c3-523d5ae1c935/";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
RegEx Circuit
jex.im visualizes regular expressions:
Reference
Do you want to match all .doc with regex, or just match the line which has a substring that matches your existing regex including the .doc?
In case of the latter, surround your regex with .*\b {regex} \b.*
This way, the whole line is matched, and the match is still captured.
^(.*\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})\b.*
Related
I am looking for help/support for a Regex expression which will match studentIdMatch2 value in below class. studentIdMatch1 matches fine.However the studentIdMatch2 has studentId which can allow all the special characters other than : and ^ and comma.Hence its not working,thank you for your time and appreciate your support.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegEx {
public static void main(String args[]){
String studentIdMatch1 = "studentName:harry,^studentId:Id123";
String studentIdMatch2 = "studentName:harry,^studentId:Id-H/MPU/L&T/OA+_T/(1490)/17#)123";
Pattern pattern = Pattern
.compile("(\\p{Punct}?)(\\w+?)(:)(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
Matcher matcher = pattern.matcher(studentIdMatch1 + ","); // Works Fine(Matches Student Name and Id)
// No Special Characters in StudentId
//Matcher matcher = pattern.matcher(studentIdMatch2 + ","); //Wont work Special Characters in StudentId. Matches Student Name
while (matcher.find()) {
System.out.println("group1 = "+matcher.group(1)+ "group2 = "+matcher.group(2) +"group3 = "+matcher.group(3) +"group4 = "+matcher.group(4)+"group5 = "+matcher.group(5));
}
System.out.println("match ended");
}
}
You may try:
^SutdentName:(\w+),\^StudenId:([^\s,^:]+)$
Explanation of the above regex:
^, $ - Represents start and end of line respectively.
SutdentName: - Matches SutdentName: literally. Although according to me it should be StudentName; but I didn't changed it.
(\w+) - Represents first capturing group matching only word characters i.e. [A-Za-z0-9_] one or more times greedily.
,\^StudenId: - Matches ,^StudenId literally. Here also I guess it should be StudentId.
([^\s,^:]+) - Represents second capturing group matching everything other than white-space, ,, ^ and : one or more times greedily. You can add others according to your requirements.
You can find the demo of the above regex in here.
Sample Implementation in java:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main
{
private static final Pattern pattern = Pattern.compile("^SutdentName:(\\w+),\\^StudenId:([^\\s,^:]+)$", Pattern.MULTILINE);
public static void main(String[] args) {
String string = "SutdentName:harry,^StudenId:Id123\n"
+ "SutdentName:harry,^StudenId:Id-H/MNK/U&T/BA+_T/(1490)/17#)123";
Matcher matcher = pattern.matcher(string);
while(matcher.find()){
System.out.println(matcher.group(1) + " " + matcher.group(2));
}
}
}
You can find the sample run of the above code in here.
The second (\\w+?) only captures words. So change it to capture what you want. i.e
allow all the special characters other than : and ^ and comma
like ([^:^,]+?)
^ - Negate the match
:^, - Matches : , ^ and comma
I need a Regex for a set including only files from /src/main/java/{any_package}/*.java to apply CheckStyle rules only to these files in Eclipse.
All other files, e.g.: none *.java files, src/test/ should be ignored.
Maybe, this expression would also function here, even though your original expression is OK.
^\/src\/main\/java(\/[^\/]+)?\/\*\.java$
Demo 1
My guess is that here we wish to pass:
/src/main/java/{any_package}/*.java
/src/main/java/*.java
If the second one is undesired, then we would simply remove the optional group:
^\/src\/main\/java(\/[^\/]+)\/\*\.java$
Demo 2
and it might still work.
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^\\/src\\/main\\/java(\\/[^\\/]+)\\/\\*\\.java$";
final String string = "/src/main/java/{any_package}/*.java\n"
+ "/src/main/java/*.java";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
RegEx Circuit
jex.im visualizes regular expressions:
The regex I'm looking for is src[\/]main\/java\/?(?:[^\/]+\/?)*.java$
Regex 101 test demo
I am trying to write a simple regex to remove all . apart from the ones which occur in real numbers.
E.g.
The value was 0.19 psi. The water level has to be brought to normal. Mtl.temp is going to be high..
The below regex selects all real numbers.
((\+|-)?([0-9]+)(\.[0-9]+)?)|((\+|-)?\.?[0-9]+)
I could do the other way wherein I could select for pattern wherein it selects . preceded by a word and succeed by space. But, the input test is not written in proper grammatical manner.
you can use the regex
\.(?!\d)
regex101 demo
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "\\.(?!\\d)";
final String string = ".12 . 0.123 Hi.there I am .invalid.";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
I am writing a program that takes in a java file and checks each line for a string containing assertEquals and then replaces the string belonging in the second argument of assertEquals (that would be expectedVar and expectedVar2).
Say these lines are read from a file and placed on a string variable:
String myString1 = "Assert.assertEquals(outputMessage, expectedVar, actualVar);"
String myString2 = "Assert.assertEquals(/"Hello World, /" + "Hello!", expectedVar2, actualVar);"
I would like to use a single regex from the Pattern Library along with 'group' and replace expectedVar and expectedVar2 or basically any string that lies in the second argument of assertEquals.
I was thinking to take in anything after the first comma and before the second comma but the myString2 could also contain multiple commas (eg. /"Hello World, /" + "Hello!").
I am not sure on how to approach this. I am willing to implement this differently if you have another idea.
Thank you in advanced
You need to use regex atomic grouping (?>...|...) >>
Java code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String text = "Assert.assertEquals(/\"Hello World, /\" + \"Hello!\", expectedVar2, actualVar)";
String pattern = "Assert\\.assertEquals\\((?>(?:[^,]*\"(?:[^,]*,)+[^,]*\")+|[^,]+),\\s*([^,]+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(text);
if (m.find()) {
System.out.println("MATCH FOUND: " + m.group(1));
} else {
System.out.println("NO MATCH");
}
}
}
Output:
MATCH FOUND: expectedVar2
Test this code here.
I get a string from a array list:
array.get(0).toString()
gives TITLE = "blabla"
I want the string blabla, so I try this :
Pattern p = Pattern.compile("(\".*\")");
Matcher m = p.matcher(array.get(0).toString());
System.out.println("Title : " + m.group(0));
It doesn't work: java.lang.IllegalStateException: No match found
I also try:
Pattern p = Pattern.compile("\".*\"");
Pattern p = Pattern.compile("\".*\"");
Pattern p = Pattern.compile("\\\".*\\\"");
Nothing matches in my program but ALL patterns work on http://www.fileformat.info/tool/regex.htm
Any Idea? Thanks in advance.
A couple of points:
The Javadoc for Matcher#group states:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
That is, before using group, you must first use m.matches (to match the entire sequence), or m.find (to match a subsequence).
Secondly, you actually want m.group(1), since m.group(0) is the whole pattern.
Actually, this isn't so important here since the regexp in question starts and ends with the capture parentheses, so that group(0) is the same string as group(1), but it would matter if your regexp looked like: "TITLE = (\".*\")"
Example code:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
#SuppressWarnings("serial")
public class MatcherTest {
#Test(expected = IllegalStateException.class)
public void testIllegalState() {
List<String> array = new ArrayList<String>() {{ add("Title: \"blah\""); }};
Pattern p = Pattern.compile("(\".*\")");
Matcher m = p.matcher(array.get(0).toString());
System.out.println("Title : " + m.group(0));
}
#Test
public void testLegal() {
List<String> array = new ArrayList<String>() {{ add("Title: \"blah\""); }};
Pattern p = Pattern.compile("(\".*\")");
Matcher m = p.matcher(array.get(0).toString());
if (m.find()) {
System.out.println("Title : " + m.group(1));
}
}
}
You need to call find() or matches() on the Matcher instance first: these actually execute the regular expression and return whether it matched or not. And then only if it matched you can call the methods to get the match groups.
are you including the double quotes (") in the string?
All your regex' have escaped "s and will only match if the string in the list includes double quote characters.