HerrPfister
HerrPfister

Reputation: 67

Finding substring in RegEx Java

Hello I have a question about RegEx. I am currently trying to find a way to grab a substring of any letter followed by any two numbers such as: d09.

I came up with the RegEx ^[a-z]{1}[0-9]{2}$ and ran it on the string

sedfdhajkldsfakdsakvsdfasdfr30.reed.op.1xp0

However, it never finds r30, the code below shows my approach in Java.

Pattern pattern = Pattern.compile("^[a-z]{1}[0-9]{2}$");
Matcher matcher = pattern.matcher("sedfdhajkldsfakdsakvsdfasdfr30.reed.op.1xp0");

if(matcher.matches())
    System.out.println(matcher.group(1));

it never prints out anything because matcher never finds the substring (when I run it through the debugger), what am I doing wrong?

Upvotes: 1

Views: 3199

Answers (5)

user1928148
user1928148

Reputation: 21

^[a-z]{1}[0-9]{2}$

sedfdhajkldsfakdsakvsdfasdfr30.reed.op.1xp0

as far as i can read this

  • find thr first lower gives[s] caps letter after it there should be two numbers meaning the length of your string is and always will be 3 word chars

Maybe if i have more data about your string i can help

EDIT

if you are sure of *number of dots then

change this line

Matcher matcher = pattern.matcher("sedfdhajkldsfakdsakvsdfasdfr30.reed.op.1xp0");

to

Matcher matcher = pattern.matcher("sedfdhajkldsfakdsakvsdfasdfr30.reed.op.1xp0".split("\.")[0]);

note:-

using my solution you should omit the leading ^ for pattern

read this page for Spliting strings

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 839214

There are three errors:

  1. Your expression contains anchors. ^ matches only at the start of the string, and $ only matches at the end. So your regular expression will match "r30" but not "foo_r30_bar". You are searching for a substring so you should remove the anchors.

  2. The matches should be find.

  3. You don't have a group 1 because you have no parentheses in your regular expression. Use group() instead of group(1).

Try this:

Pattern pattern = Pattern.compile("[a-z][0-9]{2}");
Matcher matcher = pattern.matcher("sedfdhajkldsfakdsakvsdfasdfr30.reed.op.1xp0");

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

ideone


Matcher Documentation

A matcher is created from a pattern by invoking the pattern's matcher method. Once created, a matcher can be used to perform three different kinds of match operations:

  • The matches method attempts to match the entire input sequence against the pattern.
  • The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.
  • The find method scans the input sequence looking for the next subsequence that matches the pattern.

Upvotes: 6

hologram
hologram

Reputation: 529

How about "[a-z][0-9][0-9]"? That should find all of the substrings that you are looking for.

Upvotes: 0

fge
fge

Reputation: 121840

  1. Your regex is anchored, as such it will never match unless the whole input matches your regex. Use [a-z][0-9]{2}.

  2. Don't use .matches() but .find(): .matches() is shamefully misnamed and tries to match the whole input.

Upvotes: 1

Javier Diaz
Javier Diaz

Reputation: 1830

It doesn't match because ^ and $ delimite the start and the end of the string. If you want it to be anywhere, remove that and you will succed.

Upvotes: 2

Related Questions