1

I've a regex which currently checks for a member number present in a string with memberNo between 6 and 9 in length

(?<memberNo>[0-9]{6,9})

What i need is if i get the input 123a456 that the regex ignores any alphanumeric characters in where the contiguous int values between 6 and 9 are present.

For example these should all match

123456
a123456
123a456
12345a6

etc

2
  • 2
    Why not remove all non-digits first and then match Commented Mar 18, 2021 at 17:40
  • 2
    Add an optional letter matching pattern, (?<memberNo>(?:[a-zA-Z]?[0-9]){6,9}) or (?<memberNo>(?:[a-zA-Z]*[0-9]){6,9}) Commented Mar 18, 2021 at 17:41

2 Answers 2

2

Seems

(?<memberNo>[0-9a-z]{6,9})

would match all your target strings. However, this pattern allows strings without a numeric character.

To mandate one or more numeric characters, positive lookahead could be used.

(?<memberNo>(?=.*[0-9])[1-9a-z]{6,9})

The (?=.*[0-9]) part is positive lookahead.

You can test this regex here https://regexr.com/5oua2

Sign up to request clarification or add additional context in comments.

Comments

1

You can allow any letter between the digits using

(?<memberNo>(?:[a-zA-Z]?[0-9]){6,9})

See the regex demo.

Details

  • (?: - start of a non-capturing group:
    • [a-zA-Z]? - one or zero ASCII letters
    • [0-9] - a single ASCII digits
  • ){6,9} - end of the group, repeat six to nine times.

See the regex graph:

enter image description here

1 Comment

(?<memberNo>(?:[a-zA-Z]*?[0-9]){6,9}) was what i ended up using but this got me really close to what i needed thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.