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

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

Related

kotlin/java match a number in a string with a regular expression [duplicate]

This question already has answers here:
How to extract numbers from a string and get an array of ints?
(13 answers)
Closed 1 year ago.
For example, if I have these strings, is there any way I can get 123 of all these strings, or 777 or 888?
https://www.example.com/any/123/ and
https://www.example.com/any/777/123/ and
https://www.example.com/any/777/123/888
What I mean is how to match the first or second or the third last number in the string.
You can use capture groups to solve this as
val strList = listOf("https://www.example.com/any/777/123/888", "https://www.example.com/any/123/", "https://www.example.com/any/777/123/")
val intList = mutableListOf<Int>()
val regex = Regex("/?(\\d+)")
strList.forEach { str ->
regex.findAll(str).forEach {
intList.add(it.groupValues[1].toInt())
}
}
Assuming the digits all follow a slash and nothing intervenes,
(?<=/)\d+(?=/\d+){0}$ parses the last number
(?<=/)\d+(?=/\d+){1}$ parses the second to last number
(?<=/)\d+(?=/\d+){2}$ parses the third to last,
etc.
With Java, You can make use of the Pattern and Matcher class from the java.util.regex package.
e.g for your case above, you want to match integers - use \d Predefined character class to match digits.
String str = "https://www.example.com/any/777/123/";
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(str);
for(; matcher.find(); System.out.println(matcher.group()));
In the above you loop through the String finding matches, and printing each subsequent found match.

How could we search for an exact match for a word/ words in java using regex? These words could includes parenthesis, curly braces etc [duplicate]

This question already has answers here:
Regex whitespace word boundary
(3 answers)
Closed 3 years ago.
public static void main(String args[]) {
findExactWord find = new findExactWord();
String fullString = "reports of a chemical (reaction; in the kitchen) area found a male employee suffering from nausea";
System.out.println(find.isContainExactWord(fullString, "chemical (reaction; in the kitchen)"));
}
private boolean isContainExactWord(String fullString, String partWord){
String pattern = "\\b"+partWord+"\\b";
System.out.println("Pattern : "+partWord);
Pattern p=Pattern.compile(pattern);
Matcher m=p.matcher(fullString);
return m.find();
}
I want this result to be - true.
Search input is : "chemical (reaction; in the kitchen)
this should search all characters exactly as is.
output is now : false
String pattern = partWord;
System.out.println("Pattern : " + partWord);
Pattern p = Pattern.compile(pattern, Pattern.LITERAL);
Matcher m = p.matcher(fullString);
return m.find();
now the tested version ;-)
it matches special characters and ignores newlines

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}

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)

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

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

Categories

Resources