Regex Pattern In Between Two Exact Strings - java

Hi I have this working code to detect a valid UUID pattern.
String pattern = "\\b[a-f0-9]{8}\\b-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-\\b[a-f0-9]{12}\\b";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(fileName);
This correctly detects strings like:
0101a8ef-db10-405a-a1d2-6bebdeb17625
I would like to add two exact strings on each side of this pattern like so:
FOLDER/0101a8ef-db10-405a-a1d2-6bebdeb17625.txt
Here is the code I am trying, but is not working:
String pattern = "FOLDER/\\b//[a-f0-9]{8}\\b-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-\\b[a-f0-9]{12}\\b.txt";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(uri.toString());

There is a // in the pattern that is not in the example string. Besides that, you can omit the word boundaries in between a character a-f0-9 and a - because it is implicit.
Note to escape the dot to match it literally, and you can add the word boundaries at the start and at the end to prevent partial matches.
You could update the pattern to
\\bFOLDER/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\\.txt\\b
See a regex demo.

Related

Retrieving data using java matcher and pattern

I am trying to get an alphanumeric number from a string statement for eg. UN345690 .I am using the below code.
Pattern pattern = Pattern.compile("\\d+");
Please tell me what code should I use to get the desired result.
I am trying to get an alphanumeric number
Since you already specified the regex \d+ I assume you want to extract the ordinary number 345690 from UN345690. An alphanumeric number would also include letters; but it is unclear which letters would be allowed in that case and how you would differentiate between a regular word and an alphanumeric "number".
Use a Matcher to search for your pattern in a string, then retrieve the first match.
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("UN345690");
if (matcher.find()) {
String number = matcher.group();
// do something with the extracted number
}

Regex: How to detect a pattern if an undesired sub pattern comes before the pattern?

I'm new to regex and I'm trying to use Java to detect a sequence of either: lowercase, uppercase, or digits, but not JUST digits separated by periods.
Restriction: No consecutive periods.
The sample String I have is: ###951.324.1###foo1.bar2.123proccess.this.subString
I currently have the following regex: ((\p{Alnum})+\.)+(\p{Alnum})+
I'm trying to have the pattern recognize foo1.bar2.123proccess.this.subString but my regex gives me 951.324.1 since it's a sub-pattern of the pattern I defined.
How would I go about detecting the subString foo1.bar2.123proccess.this.subString
I would imagine the general nature would be: The entire returned String should have at least 1 lowercase or uppercase char, but I'm hopelessly confused on how I would detect that in the String.
[a-zA-Z\d.]*[a-zA-Z][a-zA-Z\d.]*
This can be split into 3 parts:
[a-zA-Z\d.]* // optional sequence of letters/numbers/dots
[a-zA-Z] // MUST have a letter
[a-zA-Z\d.]* // optional sequence of letters/numbers/dots
Basically, "sandwiching" things that are required in optional things.
Try it here: https://regex101.com/r/VT4t2x/1
You may use
String rx = "\\d+(?:\\.\\d+)+|(\\p{Alnum}+(?:\\.\\p{Alnum}+)+)";
See the regex demo (pattern adjusted since regex101 does not support Java POSIX character class syntax)
The point is to match and skip dot-separated digit chunks, and only match and capture what you need. See Java demo:
String s = "###951.324.1###abc.123";
String rx = "\\d+(?:\\.\\d+)+|(\\p{Alnum}+(?:\\.\\p{Alnum}+)+)";
Pattern pattern = Pattern.compile(rx);
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
if (matcher.group(1) != null) {
System.out.println(matcher.group(1));
}
} // => abc.123

Java Regular expressions for filename

I want to check the filenames sent to me against two patterns.
The first regular expression is ~*~, which should match names like ~263~. I put this in online regular expression testers and it matches. The code doesnt work though. Says no match
List<FTPFile> ret = new ArrayList<FTPFile>();
Pattern pattern = Pattern.compile("~*~");
Matcher matcher;
for (FTPFile file : files)
{
matcher = pattern.matcher(file.getName());
if(matcher.matches())
{
ret.add(file);
}
}
return ret;
Also the second pattern I need is ##* which should match strings like abc#ere#sss
Please tell me the proper patterns in java for this.
You need to define your pattern like,
Pattern pattern = Pattern.compile("~.*~");
~* in your regex ~*~ will repeat the first ~ zero or more times. So it won't match the number following the first ~. Because matches method tries to match the whole input string, this regex causes the match to fail. So you need to add .* inbetween to match strings like ~66~ or ~kjk~ . To match the strings which has only numbers present inbetween ~, you need to use ~\d+~
Try Regex:
\~.*\~
Instead:
~*~
Example:
Pattern pattern = Pattern.compile("\\~.*\\~");

Regular expression to find substring in text

I have a text file contains some strings I want to extract with Java regex,
Those strings are in format of:
$numbers,numbers,numbers....,numbers##
(start with $, followed by groups of numbers plus ,, and end with ##)
Here is my pattern.
Pattern pattern = Pattern.compile("$*##");
Matcher matcher = pattern.matcher(text);
if (matcher.find())
{
}
It turns out that nothing match my pattern
Can anyone tell me what's wrong with it?
You need to do:
Pattern pattern = Pattern.compile("\\$\\$\\d+(,\\d+)*##$");
Thanks to #Pshemo for his valuable inputs to reach the solution.

%etd(msg01) regular expression?

I am trying to write a regular expression for String like %etd(msg01).
String string = "My name is %etd(msg01) and %etd(msg02)";
Pattern pattern = Pattern.compile("%etd(.+)");
Matcher matcher = pattern.matcher(string);
while(matcher.find()) {
System.out.println(matcher.group());
}
It prints %etd(msg01) and %etd(msg02). However, I want it to print %etd(msg01) %etd(msg02) separately. I mean I am looking for non-greedy match.
How should the regular expression be changed to make it non greedy in this situation?
You should use this regex:
Pattern pattern = Pattern.compile("%etd\\([^)]+\\)");
Please place a question mark after .* or .+ to make it nongreedy. This should work for you...
Pattern pattern = Pattern.compile("%etd\\(.+?\\)");
Double slashes are also necessary in front of open and close parenthesis because they carry a special meaning in regular expression.
Another way of using is as below if you are sure that your names doesn't contain an open paranthesis after the first one.
Pattern pattern = Pattern.compile("%etd\\([^(]+\\)");

Categories