0

I would like to find and match a pattern of character "ab" with any number. I've been trying

.*([0-9]+ab)

this seems to work but is only finding any matches at the end of a string, not in any location of the string.

testString = "if this was a test string 123ab"

TestStringNotFound = "if this 123ab string was a test"

however by using

.*([0-9]+ab).*

it seems to look at any location of the string but from my understanding . is just referring to any character and * was any digit. So I'm struggling to understand why adding .* to the end will return the specified match anywhere in the string.

I've been using this documentation as a guide gave me the impression that ([0-9]+ab) would be the way to write this expression. Thanks for the help.

  • `*` is a quantifyer (not a digit), meaning: the preceding character or group of characters zero or more times. – Chris Ruehlemann Aug 08 '21 at 12:15
  • Both strings can be found. Play with the regex in regex101.com: https://regex101.com/r/kwHEGL/2 – MDR Aug 08 '21 at 12:33
  • Why you're putting wildcards at the both ends? /(\d+ab)/g - finds every occurrences! – h-sifat Aug 08 '21 at 13:23
  • @h-sifat perhaps because [the regular expression must match the entire string](https://www.elastic.co/guide/en/elasticsearch/reference/current/regexp-syntax.html) ? – jhnc Aug 08 '21 at 16:10
  • `.` matches any char but a line feed char. `*` means "zero or more, as many times as possible", so `.*` matches any text on the line. – Wiktor Stribiżew Aug 08 '21 at 19:11

0 Answers0