0

I have a series of strings that I am searching for a particular combination of characters in. I am looking for a digit, following by the letter m or M, followed by a digit, then followed by the letter f or F.

An example string is - "Class (4) 1m5f Good" - The text in bold is what I want to extract from the string.

Here is the code I have, that doesn't work.

Pattern distancePattern = Pattern.compile("\\^[0-9]{1}[m|M]{1}[0-9]{1}[f|F]{1}$\\");
Matcher distanceMatcher = distancePattern.matcher(raceDetails.toString());
while (distanceMatcher.find()) {
 String word= distanceMatcher.group(0);
 System.out.println(word);
}

Can anyone suggest what I am doing wrong?

2
  • 4
    Remove ^ and $ ... Commented Aug 19, 2013 at 12:54
  • also, you don't need {1} after the character classes. [0-9] on its own means "once". Commented Aug 19, 2013 at 13:09

3 Answers 3

3

The ^ and $ characters at the start and end of your regex are anchors - they're limiting you to strings that only consist of the pattern you're looking for. The first step is to remove those.

You can then either use word boundaries (\b) to limit the pattern you're looking for to be an entire word, like this:

Pattern distancePattern = Pattern.compile("\\b\\d[mM]\\d[fF]\\b");

...or, if you don't mind your pattern appearing in the middle of a word, e.g., "Class (4) a1m5f Good", you can drop the word boundaries:

Pattern distancePattern = Pattern.compile("\\d[mM]\\d[fF]");

Quick notes:

  • You don't really need the {1}s everywhere - the default assumption is that a character or character class is happening once.
  • You can replace the [0-9] character class with \d (it means the same thing).
  • Both links are to regular-expressions.info, a great resource for learning about regexes that I highly recommend you check out :)
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic explanation, really appreciate it.
1

I'd use word boundaries \b:

\b\d[mM]\d[fF]\b

for java, backslashes are to be escaped:

\\b\\d[mM]\\d[fF]\\b

{1} is superfluous
[m|M] means mor | or M

1 Comment

The slashes needed escaping in Java ("\\b\\d[mM]\\d[fF]\\b"), but other than that, perfect. Thank you.
0

For the requirement of a digit, following by the letter m or M, followed by a digit, then followed by the letter f or F regex can be simplified to:

Pattern distancePattern = Pattern.compile("(?i)\\dm\\df");

Where:

(?i) - For ignore case
\\d  - For digits [0-9]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.