.netregexxval

Regular expression or operator


I'm trying to write a regular expression to test as string. The string must start or end with an alphanumeric character.

eg.

test - OK
test$ - OK
$test - OK
$ - not OK
$test$ - not OK

I can test the beginning with ^\w.*$ and the end with ^\w.*$.

But I can't seem to combine them into something like ^.*\w$ | ^\w.*$.

Does anyone have any ideas or even a better regex for this purpose?


Solution

  • This should work:

    ^\w.*|.*\w$