user2254180
user2254180

Reputation: 856

Java/Regex - finding a characters anywhere in a String

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?

Upvotes: 0

Views: 3327

Answers (3)

Michelle
Michelle

Reputation: 2900

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 :)

Upvotes: 3

Toto
Toto

Reputation: 91518

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

Upvotes: 1

anubhava
anubhava

Reputation: 786339

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]

Upvotes: 0

Related Questions