Skip to main content
August 11, 2011
Answered

Simple Regular Expression

  • August 11, 2011
  • 1 reply
  • 287 views

Hi,

I need a simple check to validate if a string contains only letters and numbers.

The regular expression to test this should be

[A-Za-z0-9]

or, to check to whole string from begin to ebd

^[A-Za-z0-9]$


Now there is REFind and REMatch to search a string for the given expressions.

The first expression does match any character, no matter where in the string.

The second expression does not match at all (although using online regex test validators do).

How do I apply this correctly to achieve my aim, namely to validate if a string contains only letters and numbers.

Thanks!

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    I would love to know which online regex testers you used that the second regex actually worked for your requirements.  So I know to avoid them as they clearly don't work.

    Both your regexes only match one character.  So the second one will only match one-character alphanumeric strings.  You want to make that a multi-character match, with a "+".

    --

    Adam

    1 reply

    Adam Cameron.Correct answer
    Inspiring
    August 11, 2011

    I would love to know which online regex testers you used that the second regex actually worked for your requirements.  So I know to avoid them as they clearly don't work.

    Both your regexes only match one character.  So the second one will only match one-character alphanumeric strings.  You want to make that a multi-character match, with a "+".

    --

    Adam