Regex

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I need to allow only characters, numbers, the dot and the
underscore ... and no spaces.

I have a regex but it is not matching a string that it should match:
@"^[a-zA-Z0-9\._]$"

What am I doing wrong?

Thanks,
Miguel
 
Hello,

I need to allow only characters, numbers, the dot and the
underscore ... and no spaces.

I have a regex but it is not matching a string that it should match:
@"^[a-zA-Z0-9\._]$"

What am I doing wrong?

I'm no regex expert, but the expression you show looks to me as though it
will match only single-character strings.

Try @"^[a-zA-Z0-9\._]*$" instead?
 
I need to allow only characters, numbers, the dot and the
underscore ... and no spaces.
I have a regex but it is not matching a string that it should match:
@"^[a-zA-Z0-9\._]$"
What am I doing wrong?

I'm no regex expert, but the expression you show looks to me as though it 
will match only single-character strings.

Try @"^[a-zA-Z0-9\._]*$" instead?

Thank You,
Miguel
 
shapper said:
Hello,
I need to allow only characters, numbers, the dot and the
underscore ... and no spaces.
I have a regex but it is not matching a string that it should match:
@"^[a-zA-Z0-9\._]$"
What am I doing wrong?
I'm no regex expert, but the expression you show looks to me as though it
will match only single-character strings.

Try @"^[a-zA-Z0-9\._]*$" instead?

Thank You,
Miguel

The \w code is the same as [0-9A-Za-z_], you can use "^[\w\.]*$"

Note that * is the same as {0,}, so it matches zero or more times. This
means that the expression also matches an empty string. If you require
at least one character, use + instead of *.
 
Back
Top