What is a regular expression for finding a string with pattern ALXXXXX in another string?

I want to search for a template string ALXXXXX inside another string.

For example, I want to find line AL00123 in the line Data Stage AL00123

I've tried the following in Java.

Pattern pattern = Pattern.compile("AL\\d\\d\\d\\d\\d");
Matcher matcher = pattern.matcher(searchString);
boolean matches = matcher.matches();

      

this always returns false. What's wrong with my code?

I want to achieve the following

  • Determine if the search string contains ALXXXXX.
  • Get ALXXXXX value.
  • I want to do this in Java.

Listed below are the different ways to use ALXXXXX in any of the following ways:

  • Data Stage_AL00123
  • Data Step AL00123
  • Data Stage (AL00123)
  • Data Stage (_AL00123)
  • Data StageAL00123

Finally

  • The line always starts with AL
  • The string will always contain 5 digits after AL
+3


source to share


2 answers


Usage Matcher#matches

matches the regex pattern against the entire input string. You want to use instead find

.



You can also reduce the regex to AL\\d{5}

.

+4


source


You are using Matcher.matches()

that only returns true

if the string matches the pattern exactly. For your purpose, you want to useMatcher.find()



+4


source







All Articles