-2

So my current regex pattern is as follows:

/^([0-9]*[a-z])*$/ gmi

I'm using regex101 to try to debug currently. My problem is the fact that this is also matching just letters. Examples of my current matches out 6 possible:

  • abc
  • 123abc
  • ABC
  • 123ABC

I only want to match the following two:

  • 123abc
  • 123ABC

What am I missing? Thank you in advance!

AnthonyFastcar
  • 153
  • 2
  • 11
  • Do you always want to match exactly 3 digits and 3 letters? Right now, your regex says "any number (0, 1, 2, 3, 4 or even more) of digits, followed by any number (0, 1, 2, 3, 4 or even more) of letters". `abc` is "0 digits followed by 3 letters; good enough!". – Amadan Nov 02 '18 at 12:55
  • Quoting regex101: "*`*` Quantifier — Matches between __zero__ and unlimited times, as many times as possible, giving back as needed (greedy)*" – melpomene Nov 02 '18 at 12:56
  • You should utilize the language for this but an engine that supports recursions will match it with `^(\d(?1)*[a-zA-Z])$` – revo Nov 02 '18 at 13:00
  • I thought you wanted to only match if the number of digits is equal to the number of letters. It does not seem to be the case. – Wiktor Stribiżew Nov 02 '18 at 13:25
  • @WiktorStribiżew Sorry for the confusion that is my fault on how I'm phrasing the question. You were correct for your previous answer. I am revising now. – AnthonyFastcar Nov 02 '18 at 13:28
  • I undeleted [my answer](https://stackoverflow.com/a/53119050/3832970). Please do not remove the language tag. – Wiktor Stribiżew Nov 02 '18 at 13:31

1 Answers1

1
/^([0-9]+[a-z]+)$/img

finds the strings in your example.

If you also need to match ABC123 (letters and numbers reversed), you can use

/^([0-9]+[a-z]+|[a-z]+[0-9]+)$/img

Tests:

123ABC           match
abd              no match
123abc           match
ABC              no match
abc123           match
ABC1234556678    match

Regex details:

"^" +              Assert position at the beginning of a line (at beginning of the string or after a line break character)
"(" +              Match the regular expression below and capture its match into backreference number 1
                   Match either the regular expression below (attempting the next alternative only if this one fails)
      "[0-9]" +    Match a single character in the range between “0” and “9”
         "+" +     Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      "[a-z]" +    Match a single character in the range between “a” and “z”
         "+" +     Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   "|" +           Or match regular expression number 2 below (the entire group fails if this one fails to match)
      "[a-z]" +    Match a single character in the range between “a” and “z”
         "+" +     Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      "[0-9]" +    Match a single character in the range between “0” and “9”
         "+" +     Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")" +
"$"                Assert position at the end of a line (at the end of the string or before a line break character)
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Do you assume `1abcdefgh` to be *half* numbers and *half* letters? – revo Nov 02 '18 at 13:06
  • Thank you Theo this is exactly what I was looking for. I appreciate the explanations provided so I know what to do for next Time. – AnthonyFastcar Nov 02 '18 at 13:07
  • @revo In this case yes I assume that. – AnthonyFastcar Nov 02 '18 at 13:08
  • Then *half* shouldn't be used to describe such requirements at all. That's not your fault but OP's. – revo Nov 02 '18 at 13:10
  • @AnthonyFastcar Question to me still open: Do you require a **minimum** or **exact** number of consecutive numbers/letters in the string? If that is the case, you can use syntax like `/^([0-9]{3,3}[a-z]{3})$/img`. Here the `+` characters are replaced by `{x,y}`. Where the _minimum_ number of characters needed is `x` and the optional _maximum_ number of characters is represented by `y`. As the regex is now, the example given by @revo will match. – Theo Nov 02 '18 at 13:12
  • @Theo The string could only be 2 characters long or it could be 100+ long. I'm just trying to account for everything. As a note I'm using "characters" in this instance to note the the whole string of alpha numeric characters in the string. Sorry for any confusion. – AnthonyFastcar Nov 02 '18 at 13:18
  • @AnthonyFastcar No worries. I though as much doing the answer, but just to be sure.. Anyway, now you know about `{x,y}` if ever you need to use that ;) – Theo Nov 02 '18 at 13:22
  • @Theo Yes thank you. That will definitely help in the future. – AnthonyFastcar Nov 02 '18 at 13:23
  • @revo Thank you for your answer too. That also helps me understand alot! – AnthonyFastcar Nov 02 '18 at 13:26