I am trying to create a general regex to extract job experience from a text.
Consider the following examples and their expected outputs.
1)String string1= "My work experience is 2 years"
Output = "2 years"
2) String string2 = "My work experience is 6 months"
Output = "6 months"
I have used regex as /[0-9] years/ but it doesn't seem to work.
Please share if anyone knows a general regex.
You can use alternations:
String str = "My work experience is 2 years\nMy work experience is 6 months";
String rx = "\\d+\\s+(?:months?|years?)";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
System.out.println(m.group(0));
}
See IDEONE demo
Output:
2 years
6 months
Or, you can also obtain strings like 3 years 6 months like this:
String str = "My work experience is 2 years\nMy work experience is 3 years 6 months and his experience is 4 years and 5 months";
String rx = "\\d+\\s+years?\\s+(?:and\\s*)?\\d+\\s+months?|\\d+\\s+(?:months?|years?)";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
System.out.println(m.group(0));
}
Output of another demo:
2 years
3 years 6 months
4 years and 5 months
I suggest using this regex:
String regex = "\\d+.*$"
Related
This question already has answers here:
How to use regex to find all overlapping matches
(5 answers)
Closed 3 years ago.
I am studying regular expression groups and have a simple question about that. Let's say I have a basic regular expression in java such as :
Pattern pattern = Pattern.compile("[0-9]{16}");
And I have a matcher :
Matcher matcher = pattern.matcher("111111111111111122);
while (matcher.find()) {
System.out.println(matcher.group());
}
When I loop, I want to be printed :
1111111111111111
1111111111111112
1111111111111122
I want to get the result of all 16 length number combinations. But it's only printed :
1111111111111111
Can I solve this issue by only modifying the regexp pattern?
To get the result you want, change your code to:
Pattern pattern = Pattern.compile("(?=([0-9]{16}))");
Matcher matcher = pattern.matcher("111111111111111122");
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Notice the call to group(1), not group(), which is the same as group(0).
Output
1111111111111111
1111111111111112
1111111111111122
This question already has answers here:
Using Regular Expressions to Extract a Value in Java
(13 answers)
Closed 6 years ago.
I have a string like this:
"text1 <text2> text3"
I want to grab only the text in <>. So I need to get text2. How can I do it?
You can do it like this:
String value = "text1 <text2> text3 <text4>";
Pattern pattern = Pattern.compile("<([^>]*)>");
Matcher matcher = pattern.matcher(value);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Output:
text2
text4
Response Update:
Assuming that you know that you have only one value to extract, Bohemian proposes a simpler approach, he proposes to proceed as next:
String value = "text1 <text2> text3";
String target = value.replaceAll(".*<(.*)>.*", "$1");
System.out.println(target);
Output:
text2
I suggest you to split this string using " " as a separator - you will get 3 -elements array and the second one is what you are looking for
I have a text file where it has information of a person. I have written a regex to extract age of a person ie X years Y months.
String n="Mayur is 18 years 4 months old ";
Pattern p=Pattern.compile("[\\d+\\s+years]+[\\d+\\s+months]+",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(n);
while (m.find()) {
System.out.println(m.group(0));
}
Output i received is:
r
s 18 years 4 months o
I did not extracted those wanted characters in the output but it is listed them.
Expected output is:
18 years 4 Month
Please not they are records with only years and some with only months.
The problem with your regex is that [\d+\s+years] matches any character found in the list so that's why you got r in the result you don't have to use brackets [].
This is the Regex you need (\\d+\\s* years\\s*)*(\\d+\\s* months)*, use () for a matching group.
I changed \\s+ to \\s* to make it match cases where it's written:
Mayur is 18years 4months old
Here's a Live DEMO
EDIT:
The problem of empty strings is due to to the *quantifier after the matching groups, I fixed it using this new Regex:
(\\d+\\s* years\\s*)+|(\\d+\\s* months)+
See the DEMO here
(?:\\d+\\s+(?:years|months)\\s*){1,2}
Use this.[] is not what you think.Its a character class.See demo.
https://regex101.com/r/uE3cC4/25
Try this:
String n="Mayur is 18 years 4 months old ";
Pattern p=Pattern.compile("([0-9]+) years ([0-9]+) months",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(n);
while (m.find()) {
String years = m.group(1);
String months = m.group(2);
System.out.println(m.group(0));
}
Using "0" as group you can get the whole expression. Otherwise using 1 or 2 you can get the values.
This question already has an answer here:
SCJP6 regex issue
(1 answer)
Closed 7 years ago.
Just a quick question about Java regex patterns! So say if I had a method like..
public void example()
{
Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("ab34ef");
boolean b = false;
while (b = m.find())
{
System.out.println(m.start() + " " + m.group());
}
}
If I ran this I would end up with the following output..
0
1
2 34
4
5
6
I understand how this works apart from how it ends up at 6, I thought it would finish on 5 could someone please explain this to me? Thanks!
In your string, "ab34ef", there are 7 "empty characters" with a value of "". They are located between each of the normal characters. It attempts to find a match starting on each empty character, not each normal character; i.e. the location of each | in the following: "|a|b|3|4|e|f|".
This question already has answers here:
Java Regex does not match
(6 answers)
Closed 9 years ago.
I know that this kind of questions are proposed very often, but
I can't figure out why this RegEx does not match.
I want to check if there is a "M" at the beginning of the line, or not.
Finally, i want the path at the end of the line.
This is why startsWith() doesn't fit my Needs.
line = "M 72208 70779 aab src\com\aut\testproject\TestDomainf1.java";
if (line.matches("^(M?)(.*)$")) {}
I've also tried the other way out:
Pattern p = Pattern.compile("(M?)");
Matcher m = datePatt.matcher(line);
if (m.matches()) {
System.out.println("yay!");
}
if (line.matches("(M?)(.*)")) {}
Thanks
Seems to be simple:
if (line.startsWith("M")) {
String[] tokens = line.split("\\s+");
String path = tokens[tokens.length - 1];
}