-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
  • 174,939
  • 50
  • 355
  • 478
AHH
  • 981
  • 2
  • 10
  • 26

2 Answers2

1

I guess you want:

^\d+

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

maio290
  • 6,440
  • 1
  • 21
  • 38
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

rlaphoenix
  • 136
  • 8