-3

Right now, I have a regular expression that looks like this:

[A-Za-z][A-Za-z0-9]*

It matches all alphanumeric characters currently. I want it to be able to match with an expression that has a middle underscore, but not if there are consecutive underscores, leading underscores, or trailing underscores. Ie

  • test__ (not ok)
  • _test (not ok)
  • test_ (not ok)
  • test_string (ok)

What would the regular expression look like?

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
DeviousKoala
  • 370
  • 1
  • 5
  • 15

2 Answers2

1

Try this

 ^[a-zA-Z0-9]+_[a-zA-Z0-9]+$

To make _ optional

^[a-zA-Z0-9]+_{0,1}[a-zA-Z0-9]+$

To allow more than one _

^(?:[a-zA-Z0-9]+_{0,1})+[a-zA-Z0-9]+$
ʞᴉɯ
  • 5,376
  • 7
  • 52
  • 89
  • Note that this solution **requires** and a middle underscore which is not the same as **allowing** a middle underscore. This doesn't make the answer wrong, but you should take this in consideration when using this answer. Further more it doesn't allow more then one underscore, so `this_wont_match`. – 3limin4t0r Nov 01 '19 at 18:53
  • Yeah, this doesn't fit OP's requirements according to the comments on the question, so I'm not sure why this was selected as the answer. – CAustin Nov 01 '19 at 18:55
  • You guys are right. I just tried it out and it didn't work as expected. I'll still pick a better answer. – DeviousKoala Nov 01 '19 at 18:56
  • @3limin4t0r so it can do ^[a-zA-Z0-9]+_{0,1}[a-zA-Z0-9]+$ – ʞᴉɯ Nov 01 '19 at 18:56
  • @DeviousKoala see edits – ʞᴉɯ Nov 01 '19 at 19:06
  • You could replace `_{0,1}` with `_?` to shorten it a bit. – 3limin4t0r Nov 01 '19 at 19:08
0
[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*

Should meet the requirements. This uses what you already had and adds the option to use an underscore if followed by at least one or more [A-Za-z0-9] characters.

regex diagram

You might want to anchor the solution with ^ and $ to the beginning and ending of the string, but this depends on your use-case.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
  • 1
    Can get slightly better performance with `^(?!\d)[A-Za-z0-9]+(?:_[A-Za-z0-9]+)*$` but I like the approach. +1 –  Nov 01 '19 at 20:51
  • @x15 Hadn't thought of that, I'll leave it as a comment though since I'll have to regenerate the diagram while the result is the same. – 3limin4t0r Nov 01 '19 at 21:00