Split a text with regex in Java - java

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.

Related

use regex to map TY_111.22-L007-C010 from a text

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.

Java Regex Alphabet Followed By Number

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

RegEx for matching mp3 URLs

How can I get an mp3 url with REGEX?
This mp3 url, for example:
https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
This is a what I've tried so far but I want it to only accept a url with '.mp3' on the end.
(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]
This expression would likely pass your desired inputs:
^(https?|ftp|file):\/\/(www.)?(.*?)\.(mp3)$
If you wish to add more boundaries to it, you can do that. For instance, you can add a list of chars instead of .*.
I have added several capturing groups, just to be simple to call, if necessary.
RegEx
If this wasn't your desired expression, you can modify/change your expressions in regex101.com.
RegEx Circuit
You can also visualize your expressions in jex.im:
const regex = /^(https?|ftp|file):\/\/(www.)?(.*?)\.(mp3)$/gm;
const str = `https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
http://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
http://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
ftp://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
file://localhost/examples/mp3/SoundHelix-Song-1.mp3
file://localhost/examples/mp3/SoundHelix-Song-1.wav
file://localhost/examples/mp3/SoundHelix-Song-1.avi
file://localhost/examples/mp3/SoundHelix-Song-1.m4a`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Java Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^(https?|ftp|file):\\/\\/(www.)?(.*?)\\.(mp3)$";
final String string = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3\n"
+ "http://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3\n"
+ "http://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3\n"
+ "ftp://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3\n"
+ "file://localhost/examples/mp3/SoundHelix-Song-1.mp3\n"
+ "file://localhost/examples/mp3/SoundHelix-Song-1.wav\n"
+ "file://localhost/examples/mp3/SoundHelix-Song-1.avi\n"
+ "file://localhost/examples/mp3/SoundHelix-Song-1.m4a";
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));
}
}
If you want it to match inputs ending with '.mp3' you should add \.mp3$ at the end of your regex.
$ indicates the end of your expression
(https?|ftp|file):\/\/[-a-zA-Z0-9+&##\/%?=~_|!:,.;]*[-a-zA-Z0-9+&##\/%=~_|]\.mp3$
Matching:
https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3 **=> Match**
https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp4 **=> No Match**
You could use anchors to assert the start ^ and the end $ of the string and end the pattern with .mp3:
^https?://\S+\.mp3$
Explanation
^ Assert start of string
https?:// Match http with optional s and ://
\S+ Match 1+ times a non whitespace char
\.mp3 Match .mp3
$ Assert end of string
Regex demo | Java demo
For example:
String regex = "^https?://\\S+\\.mp3$";
String[] strings = {
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp4"
};
Pattern pattern = Pattern.compile(regex);
for (String s : strings) {
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
System.out.println(matcher.group(0));
}
}
Result
https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3

How to use REGEX to validate EditText?

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]+)*$/

regular expression to cut text [duplicate]

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+.]*)$

Categories

Resources