Regex

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
 
P

Peter Duniho

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?
 
S

shapper

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
 
G

Göran Andersson

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 *.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Replace email 1
Obfuscate 4
Obfuscate Email 1
RegEx Help 2
Regex. Reject space 3
Regex. Digits, Letters and Dashes. what am I doing wrong? 6
Regex wiseguy or girl 2
Are you a RegEx bandido? 2

Top