-1

I am trying to extract a number (Must exist, any length) from a string. The string should actually start with the number. I am using this expression:

[[\d]+YMD]

When given 11M as input, the 11 is matched twice as 1 then 1. What am I missing in this RegEx?

I am writing a web application, but for now, I am testing the RegEx online where I am getting the same results.

Thomas
  • 150,847
  • 41
  • 308
  • 421
AHH
  • 902
  • 2
  • 9
  • 22

2 Answers2

1

I guess you want:

^\d+

^ = start of string \d = any digit + = 1 or more

maio290
  • 5,118
  • 1
  • 14
  • 29
1

You're most likely looking for the + quantifier which: "Matches between one and unlimited times, as many times as possible, giving back as needed".

^\d+(D|M|Y) will specifically give you what you seem to be looking for (making it only match if it starts with a number and ends with D, M, or Y, making sure there's at least 1 number.

https://regex101.com/r/gWxkhd/2

PHOENiX
  • 36
  • 6