Regex searching for number and letter combination optional brackets -


i need regex find match of single lower case a-z character followed 5 numbers either:

  • at start of line
  • at end of line
  • surrounded () or []
  • surrounded whitespace

so following results expected:

a12345 match

(a12345) match

[a12345] match

text a12345 match

aa12345 no match

at moment have (?<=[])]*)[a-z]{1}[0-9]{5}(?=[])]*) not working scenarios, example sees aa12345 , a12345a being matches when don't want them to.

can help?

edit: apologies should have mentioned .net c#

first of should mention programming language.

following solution pcre.

regex: ((?<=[\[( ])|^)[a-z]\d{5}((?=[\]\) ])|$)

explanation:

  1. ((?<=[\[( ])|^) checks preceding brackets, whitespaces or beginning.

  2. [a-z]\d{5} checks alphabet followed 5 digits.

  3. ((?=[\]\) ])|$) checks succeeding brackets, whitespaces or end of line.

regex101 demo


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -