Jump to content

Java Regex Help

yuh25
Go to solution Solved by wolfsinner,

The following pattern should capture what you need: \\d{1,2}[A-Za-z]{3}

 

EDIT:

I failed to realize you probably also want some code to go with that. :P

The following code is untested, but gives you the general idea.

String pattern = "(\\d{1,2}[A-Za-z]{3})";Pattern p = Pattern.compile(pattern);Matcher m = p.matcher(htmlStr);if(m.find()){         for(int i = 1 ; i <= m.groupCount() ; i++){                  //treat m.group(i)         }}

So I'm doing this program that requires me to extract strings from a HTML page.

I'm very new to regexs and was wondering if there was a way to find:

1 or 2 digit integers and 3 chars.

eg: 8SCK or 10SCA

It also has to be able to avoid picking up something like this: BAABA

Thanks.

3700x, Asus B450i, 16GB Corsair Vengeance RGB, Gigabyte GTX 970 ITXDan A4-SFX, SX600-G, 120mm AIO.

Link to comment
Share on other sites

Link to post
Share on other sites

The following pattern should capture what you need: \\d{1,2}[A-Za-z]{3}

 

EDIT:

I failed to realize you probably also want some code to go with that. :P

The following code is untested, but gives you the general idea.

String pattern = "(\\d{1,2}[A-Za-z]{3})";Pattern p = Pattern.compile(pattern);Matcher m = p.matcher(htmlStr);if(m.find()){         for(int i = 1 ; i <= m.groupCount() ; i++){                  //treat m.group(i)         }}

Want to solve problems? Check this out.

Link to comment
Share on other sites

Link to post
Share on other sites

Perfect Thanks  :)

3700x, Asus B450i, 16GB Corsair Vengeance RGB, Gigabyte GTX 970 ITXDan A4-SFX, SX600-G, 120mm AIO.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×