regex to split string like "ABC123XYZ111" in java [duplicate] - java

This question already has an answer here:
Split string in alphabet and number
(1 answer)
Closed 6 years ago.
I have a string like ABC123XYZ111 so that regex should be able to split last numeric part as one part and from starting to start of last numberic value si one part,
eg : I want to split into two parts
first part should contain ABC123XYZ and
second part should be 111
first part may contain numeric values but second part should be numeric.

This worked for me
myString.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");

Keep it simple then.
String root = s.replaceAll("^(.*?)(\\d+)$", "$1");
String number = s.replaceAll("^(.*?)(\\d+)$", "$2");
^(.*?)(\\d+)$
^ = begin of string
$ = end of string
.*? = shortest sequence of any character
\d+ = digit, one or more
( ... ) = group numbered from 1, $1, $2, ...

You can use this regex
(.*?)(?=\d+$)(.*)
Java Code
String line = "ABC123XYZ111";
String pattern = "(.*?)(?=\\d+$)(.*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
}
IDEONE DEMO

Related

Extract numbers from a string based on a another string [duplicate]

This question already has answers here:
Java Regex group 0
(2 answers)
Closed 4 years ago.
I am trying to write a regex for a string which has a format [digit] [to] [digit] eg. 1 to 5 in which if I find a word "to" from a given string i want to extract the number before and after, I have tried this and it's not working.
Pattern p = Pattern.compile("([0-9]+)\\bto\\b([0-9]+)");
Matcher m = p.matcher("1 to 5");
m.find();
System.out.println(m.group(0));
System.out.println(m.group(1));
System.out.println(m.group(2));
Expected o/p
1
to
5
Consider adding a group for the to part.
Also for the space, you want \\s not \\b:
Pattern p = Pattern.compile("([0-9]+)\\s(to)\\s([0-9]+)");
Matcher m = p.matcher("1 to 5");
m.find();
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
And as said in the comments :
" Group zero denotes the entire pattern"
Is it necessary that you must use regex. If not, you can use String functions.
String s="23 to 34";
String toString="to";
if(s.contains(toString)){
int startIndex=s.indexOf(toString);
int endIndex=startIndex+(toString).length();
String s1=s.substring(0, startIndex); //get the first number
String s2=s.substring(endIndex); //get the second number
System.out.println(s1.trim()); // Removing any whitespaces
System.out.println(toString);
System.out.println(s2.trim();
}

Extracting a word from a message in java [duplicate]

This question already has answers here:
Java Find word in a String
(5 answers)
Find word in random string
(3 answers)
How to find index of whole word in string in java
(1 answer)
Regex to find a specific word in a string in java
(3 answers)
Closed 5 years ago.
I have a message that is of the format:
FixedWord1 variable1 FixedWord2 on FixedWord3 variable2/variable3, variable4 = variable5
I need to extract only variable3 from the above message.
Here is what I tried:
String example = "FixedWord1 variable1 FixedWord2 on FixedWord3 variable2/variable3, variable4 = variable5";
I know that the length of FixedWord3 is 6. So,
example.substring(example.lastIndexOf("FixedWord3") + 6 , example.lastIndexOf(",")); //To get {variable2}/{variable3}
And then,
String requiredString[] = example.split("/", 2); //requiredString[1] would contain {variable3} even if it contains /
Can you suggest a more efficient solution to this problem?
EDIT:
This regex should do the trick.
Pattern pattern = Pattern.compile(".+(Device).+[/]([A-Z].+)[,][ ].+");
Matcher matcher = pattern.matcher(yourstring);
if(matcher.matches())
System.out.println(matcher.group(2));
Assumption to make this work:
Variable2 has no slash '/' followed by upper case letter
Variable3 has no comma ',' followed by space ' '
Since you know that variable2 cannot contain a "/" and you know the length of FixedWord3 then how about this?
String deviceName = example.substring(example.lastIndexOf("Device") + 6, example.lastIndexOf(","));
String lastPart = deviceName.substring(deviceName.indexOf("/") + 1);
System.out.println(deviceName);
System.out.println(lastPart);
Prints:
SJ-ME3600X-185/GigabitEthernet0/4
GigabitEthernet0/4
Regex for the help.
One possible approach is catching the match that's after "{variable2}":
{variable2}\/{([^}]+)}
Then you can use Matcher and Pattern and maybe other tools to make it work in Java.
See here for explanation and live demo.
Using Regex patterns are the efficient way to extract the word from a message in java.
String s = "FixedWord1 {variable1} FixedWord2 on FixedWord3 {variable2}/{variable3}, {variable4} = {variable5}";
Pattern p = Pattern.compile("/(\\{([^}]*)\\})");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
Output {variable3}

add +n to the name of some Strings id ends with "v + number"

I have an array of Strings
Value[0] = "Documento v1.docx";
Value[1] = "Some_things.pdf";
Value[2] = "Cosasv12.doc";
Value[3] = "Document16.docx";
Value[4] = "Nodoc";
I want to change the name of the document and add +1 to the version of every document. But only the Strings of documents that ends with v{number} (v1, v12, etc).
I used the regex [v]+a*^ but only i obtain the "v" and not the number after the "v"
If all your strings ending with v + digits + extension are to be processed, use a pattern like v(\\d+)(?=\\.[^.]+$) and then manipulate the value of Group 1 inside the Matcher#appendReplacement method:
String[] strs = { "Documento v1.docx", "Some_things.pdf", "Cosasv12.doc", "Document16.docx", "Nodoc"};
Pattern pat = Pattern.compile("v(\\d+)(?=\\.[^.]+$)");
for (String s: strs) {
StringBuffer result = new StringBuffer();
Matcher m = pat.matcher(s);
while (m.find()) {
int n = 1 + Integer.parseInt(m.group(1));
m.appendReplacement(result, "v" + n);
}
m.appendTail(result);
System.out.println(result.toString());
}
See the Java demo
Output:
Documento v2.docx
Some_things.pdf
Cosasv13.doc
Document16.docx
Nodoc
Pattern details
v - a v
(\d+) - Group 1 value: one or more digits
(?=\.[^.]+$) - that are followed with a literal . and then 1+ chars other than . up to the end of the string.
The Regex v\d+ should match on the letter v, followed by a number (please note that you may need to write it as v\\d+ when assigning it to a String). Further enhancement of the Regex depends in what your code looks like. You may want to to wrap in a Capturing Group like (v\d+), or even (v(\d+)).
The first reference a quick search turns up is
https://docs.oracle.com/javase/tutorial/essential/regex/ ,
which should be a good starting point.
Try a regex like this:
([v])([1-9]{1,3})(\.)
notice that I've already included the point in order to have less "collisions" and a maximum of 999 versions({1,3}).
Further more I've used 3 different groups so that you can easily retrieve the version number increase it and replace the string.
Example:
String regex = ;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(time);
if(matcher.matches()){
int version = matcher.group(2); // don't remember if is 0 or 1 based
}

Need a Regex that extracts a string between two "delimiting" strings [duplicate]

This question already has answers here:
Java Regex Capturing Groups
(4 answers)
Closed 6 years ago.
I need to get the string between by_ and _on.
So far I have this, but don't understand how to truncate the actual "string delimiters":
by_(.*)_on
Sample input:
Files_by_wesasegeaazedude_on_January_26.jpg
Current Match:
by_wesasegeaazedude_on
Needed Match:
wesasegeaazedude
Your expression is good*. All you need to do is extracting the content of the first capturing group:
Pattern regex = Pattern.compile("by_(.*)_on");
String str = "Files_by_wesasegeaazedude_on_January_26.jpg";
Matcher m = regex.matcher(str);
if (m.find()) {
String res = m.group(1);
}
Demo.
* Well, almost good. If you expect inputs with multiple file names on the same line, you may want to consider using reluctant qualifier, i.e. by_(.*?)_on
I would do this without regular expressions.
int start = str.indexOf("by_");
int end = str.indexOf("_on", start + 1); // or lastIndexOf("_on"), for greedy match.
assert start > 0 && end > start;
String part = str.substring(start + 3, end);
You can simply use positive lookarounds:
String regex = "(?<=by_).*(?=_on)";
What this regex does is:
match anything: .*
that is preceded by by_: (?<=by_)
and followed by _on: (?=_on)

extract a substring in Java

I have the following string:
"hello this.is.a.test(MainActivity.java:47)"
and I want to be able to extract the MainActivity.java:47
(everything that is inside '(' and ')' and only the first occurance).
I tried with regex but it seems that I am doing something wrong.
Thanks
You can do it yourself:
int pos1 = str.indexOf('(') + 1;
int pos2 = str.indexOf(')', pos1);
String result = str.substring(pos1, pos2)
Or you can use commons-lang which contains a very nice StringUtils class that has substringBetween()
I think Regex is a liitle bit an overkill. I would use something like this:
String input = "hello this.is.a.test(MainActivity.java:47)";
String output = input.subString(input.lastIndexOf("(") + 1, input.lastIndexOf(")"));
This should work:
^[^\\(]*\\(([^\\)]+)\\)
The result is in the first group.
Another answer for your question :
String str = "hello this.is.a.test(MainActivity.java:47) another.test(MyClass.java:12)";
Pattern p = Pattern.compile("[a-z][\\w]+\\.java:\\d+", Pattern.CASE_INSENSITIVE);
Matcher m=p.matcher(str);
if(m.find()) {
System.out.println(m.group());
}
The RegExp explained :
[a-z][\w]+\.java:\d+
[a-z] > Check that we start with a letter ...
[\w]+ > ... followed by a letter, a digit or an underscore...
\.java: > ... followed exactly by the string ".java:"...
\d+ > ... ending by one or more digit(s)
Pseudo-code:
int p1 = location of '('
int p2 = location of ')', starting the search from p1
String s = extract string from p1 to p2
String.indexOf() and String.substring() are your friends.
Try this:
String input = "hello this.is.a.test(MainActivity.java:47) (and some more text)";
Pattern p = Pattern.compile("[^\\)]*\\(([^\\)]*)\\).*");
Matcher m = p.matcher( input );
if(m.matches()) {
System.out.println(m.group( 1 )); //output: MainActivity.java:47
}
This also finds the first occurence of text between ( and ) if there are more of them.
Note that in Java you normally have the expressions wrapped with ^ and $ implicitly (or at least the same effect), i.e. the regex must match the entire input string. Thus [^\\)]* at the beginning and .* at the end are necessary.

Categories

Resources