16

For instance:

'1'     => NG
'243'   => NG
'1av'   => OK
'pRo'   => OK
'123k%' => NG

I tried with

 /^(?=^[^0-9]*$)(?=[a-zA-Z0-9]+)$/

but it is not working very well.

hello_there_andy
  • 2,039
  • 2
  • 21
  • 51
oldergod
  • 15,033
  • 7
  • 62
  • 88

7 Answers7

30

Use

/^(?![0-9]*$)[a-zA-Z0-9]+$/

This expression has a negative lookahead to verify that the string is not only numbers. See it in action with RegExr.

Jens
  • 25,229
  • 9
  • 75
  • 117
  • In regexr this works but in Java using Validation @Pattern(regex="...") it doesn't work for me. Maybe this is not available in all regex implementations? – Michael Apr 11 '18 at 10:15
13

So we know that there must be at least one "alphabetic" character in there somewhere:

[a-zA-Z]

And it can have any number of alphanumeric characters (including zero) either before it or after it, so we pad it with [a-zA-Z0-9]* on both sides:

/^[a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*$/

That should do the trick.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • Is there an easy way to limit this expression also with sting length? – zygimantus Jan 13 '23 at 15:18
  • @zygimantus Not this expression, no - since the total length could be split in multiple ways across the two(/three) parts of this expression. You could do it more easily with the accepted answer (replace the `+` with e.g. `{1,80}` for a line less than 80 chars); but that said regex is a poor tool for validating length, and if possible I'd just check it directly (e.g. `line.length() < 80`) before matching the regex. – Andrzej Doyle Jan 16 '23 at 10:50
  • Thanks for reply, yes, I thought same, to check length of a string directly in code. – zygimantus Jan 17 '23 at 20:15
4

Try with this:

/^(?!^\d*$)[a-zA-Z\d]*$/

Edit: since this is essentially the same of the accepted answer, I'll give you something different:

/^\d*[a-zA-Z][a-zA-Z\d]*$/

No lookaheads, no repeated complex group, it just verifies that there's at least one alphabetic character. This should be quite fast.

MaxArt
  • 22,200
  • 10
  • 82
  • 81
  • The second one worked, it excludes only numbers, thanks! It still doesn't exclude only alphabets. – Pavitar Dec 24 '19 at 05:57
1

try with this: ^(?!\d+\b)[a-zA-Z\d]+$
(?!\d+\b) will avoid pure numeric, add \w+ then mix alphabet and number.

godspeedlee
  • 672
  • 3
  • 7
  • thank you for your comment, it should change to ^(?!\d+\b)[a-zA-Z\d]+$ – godspeedlee Aug 02 '12 at 08:47
  • Well you can edit your answer. If you do, you'll get a +1 from me. – MaxArt Aug 02 '12 at 09:00
  • 1
    Actually no, `\b` was a good idea. With that, the regex test fails before with unvalid strings like "123#test", because the lookahead fails first. Using `$` the lookahead doesn't fail. – MaxArt Aug 02 '12 at 09:15
0

This should do the trick, without lookbehind:

^(\d*[a-zA-Z]\d*)+$
davidrac
  • 10,723
  • 3
  • 39
  • 71
0

Try this: ^[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*$

OnlineCop
  • 4,019
  • 23
  • 35
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Oct 22 '15 at 11:10
0

this is strictly alphanumeric

String name="^[^a-zA-Z]\\d*[a-zA-Z][a-zA-Z\\d]*$";
J. Chomel
  • 8,193
  • 15
  • 41
  • 69