This question already has answers here:
Match exact string
(3 answers)
Closed 3 years ago.
I am creating a regex to support the following patterns
Documents/OneDrive/Collections/book.xlsx
Documents/OneDrive/Collections/book.xls
Documents/OneDrive/Collections/book.xlsm
book 2.xls
aa.xlsx
Attempt
^([a-zA-Z0-9 ]+[/])*([a-zA-Z0-9 ])
"+[.](xls[xm]?"
This regex matches all the required patterns but how can i just limit to the last character.
My guess is that you might want an expression similar to:
^(([a-z0-9\s]+\/)+)?([a-z0-9\s]+)\.[a-z]+
or:
^(([a-z0-9\s]+\/)+)?([a-z0-9\s]+)\.(xlsx?m?)$
Demo 1
Demo 2
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^(([a-z0-9\\s]+\\/)+)?([a-z0-9\\s]+)\\.[a-z]+";
final String string = "Documents/OneDrive/Collections/book.xlsx\n"
+ "Documents/OneDrive/Collections/book.xls\n"
+ "Documents/OneDrive/Collections/book.xlsm\n"
+ "book 2.xls\n"
+ "aa.xlsx\n\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
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:
Related
I want to get TY_111.22-L007-C010,Tzo11-L010-C100 and Tff-L010-C110 from this string with regex
"12.5*MAX(\"TY_111.22-L007-C010\";\"Tzo11-L010-C100\";\"Tff-L010-C110\")
I tested this T.*-L\d*-C\d* but it don't give the result I want :
My code java for test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "T.*-L\\d*-C\\d*";
final String string = "\"12.5*MAX(\\\"TY_111.22-L007-C010\\\";\\\"Tzo11-L010-C100\\\";\\\"Tff-L010-C110\\\"";
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));
}
}
You need to use this regex T.*?\-L\d*?\-C\d*
final String regex = "T.*?\\-L\\d*?\\-C\\d*";
Note: you need to escape the hyphens \- and use non-greedy quantifier .*? instead of .*, also you can use only matcher.group() instead of matcher.group(0), in your regex you don't have any groups, so the 0 is useless.
Outputs
Full match: TC_24.00-L010-C090
Full match: TC_24.00-L010-C100
Full match: TC_24.00-L010-C110
Why use a verbose regex pattern matcher when you can handle the problem with one line of code:
String input = "12.5*MAX(\"Txxxx-L007-C010\";\"Txxxx-L010-C100\";\"Txxxx-L010-C110\")";
String[] matches = input.replaceAll("^.*?\"|\"[^\"]*$", "")
.split("\";\"");
System.out.println(Arrays.toString(matches));
This prints:
[Txxxx-L007-C010, Txxxx-L010-C100, Txxxx-L010-C110]
OK...I used three lines of code, but the first and third are just for setting up the data and printing it.
I am getting file names as string as follows:
file_g001
file_g222
g_file_z999
I would like to return files that contains "g_x" where x is any number (as string). Note that the last file should not appear as the g_ is followed by an alphabet and not a number like the first 2.
I tried: file.contains("_g[0-9]*$") but this didn't work.
Expected results:
file_g001
file_g222
Are you using the method contains of String ?
If so, it does not work with regular expression.
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contains-java.lang.CharSequence-
public boolean contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.
Consider using the method matches.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#matches(java.lang.String)
Your regular expression is also fine, we'd just slightly improve that to:
^.*_g[0-9]+$
or
^.*_g\d+$
and it would likely work.
The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^.*_g[0-9]+$";
final String string = "file_g001\n"
+ "file_g222\n"
+ "file_some_other_words_g222\n"
+ "file_g\n"
+ "g_file_z999";
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));
}
}
I need to get a text splitted with regex in Java (each substring will be less than or close to 10 characters (including space and special) and no word would be splitted). For example, "James has gone out for a meal." would be "James has", "gone out", "for a meal", ".".
Thanks in advance.
This expression might be a little complicated, maybe we could start with:
.{1,10}[^\s](?=\s|$)
DEMO
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = ".{1,10}[^\\s](?=\\s|$)";
final String string = "James has gone out for a meal.";
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:
First, remove all double spaces if exists and apply this regex.
.{1,11}(?:\s|$)|.{1,11}(?:[^\s]|$)
But I would use the split function and then 'for clause' calculating lengths.
I want to validate a username against these requirements:
Just accept character or digital
At least one character
I tried with
public boolean validateFormat(String input){
return Pattern.compile("^[A-Za-z0-9]+$").matcher(input).matches();
}
How can I do this one?
Try with this regex:
^(\w|\d)+$
^ indicates the start of the string
$ indicates the end of the string
\w means any word character
\d means any digit
| is the logical OR operator
Anyway, i suggest you to use an online regex tester like regex101.com .It is very helpful to quickly test regular expressions.
Hope it can help!
== UPDATE ==
In Java code:
final String regex = "^(\\w|\\d)+$";
final String string = "myCoolUsername12";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
if(matcher.matches()) {
// if you are interested only in matching the full regex
}
// Otherwise, you can iterate over the matched groups (including the full match)
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));
}
}
/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/
This question already has answers here:
Java: splitting a comma-separated string but ignoring commas in parentheses
(4 answers)
Closed 5 years ago.
I want to cut this text
UNIT=1111,SPACE=(TRK,0),DISP=(MOD,DELETE,DELETE),DSN=UUU.AAAAA.BBBBB
Result :
UNIT=1111
SPACE=(TRK,0)
DISP=(MOD,DELETE,DELETE)
DSN=UUU.AAAAA.BBBBB
I tried myself but I m so noob with regular expression, I used (\S+)=(\S+) to cut it but it not work correct.
Someone could help me ?
Here is my java code
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "(\\S+)=(\\S+)";
final String string = "UNIT=1111,SPACE=(TRK,0),DISP=(MOD,DELETE,DELETE),DSN=UUU.AAAAA.BBBBB"
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));
}
}
You can use this negative lookahead regex for splitting:
String[] arr = str.split(",(?![^()]*\\))");
This is assuming ( and ) are all balanced and unescaped.
RegEx Demo
RegEx Breakup:
,: Match a literal comma
(?![^()]*\\)): Negative lookahead to assert that comma is not inside a (...)
Working code ise here https://regex101.com/r/G355nS/2
^(UNIT=\d{4}),(SPACE=\S+\d+\)),(DISP=\S+\)),(DSN=[\S+.]*)$