Xingsheng Guo
Xingsheng Guo

Reputation: 71

Java extract text from job descriptions (Regex OR Pattern)

In my text there will be content like:

2 years of experiences of databse, XXXBBXBXB 3 years databse and sql experiences, 
UUYFS 3 year experiences, 4 yeears databse contract, 5 years contract

What I want is to find the pattern of and get the single digit before the pattern:

1: years of experience
2: year of experiences
...

There will be the case that some text will be between the 'years' and 'experience'. And it might be appear as 'years' OR 'year' OR 'year experiences' and so on.

But the final output will be looks like: (excluding the digits with other patterns like 'years contract' and so on)

2, 3, 3

I tried something like '\years\experience', but seems it is wrong.

Any help? Thanks

Upvotes: 0

Views: 188

Answers (2)

Braj
Braj

Reputation: 46891

Try with below regex

(\d+)\s+(year|years)

enter image description here

Get the digit at group 1 using Matcher#group() that looks for the groups enclosed in parenthesis ().

Read more about Java Regex Pattern

DEMO

Sample code:

String url = "2 years of experiences of databse, XXXBBXBXB 3 years databse and sql experiences, UUYFS 3 year experiences";
Pattern pattern = Pattern.compile("(\\d+)\\s+(year|years)");
Matcher matcher = pattern.matcher(url);

while (matcher.find()) {
    System.out.println(matcher.group(1));
}

output:

2
3
3

EDIT:

Try below pattern as per your comment:

(\d+)\s+(years of experience|year experience)

Upvotes: 2

Robby Cornelissen
Robby Cornelissen

Reputation: 97381

I think you're looking for something like this:

(\d+)\s+years?\s+.*?\s+experience

Upvotes: 1

Related Questions