简体   繁体   中英

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

Try with below regex

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

在此处输入图片说明

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)

我认为您正在寻找这样的东西:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM