Regular Expression

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

Why does this work?

if (Regex.IsMatch("A234456", @"[ABCEGHJKLMNPRSTVXY]\d{1}"))
temp = temp;
else
temp = temp;

Doesn't the {1} say only one digit?

If I said:

@"[0-9]{1}

This would say only 1 digit (where the {1} is not necessary as that is the
default)

But

@"[ABCEGHJKLMNPRSTVXY]{2}

Says 2 digits

Doesn't the {} say how many of the previous set?

Thanks,

Tom
 
Why does this work?

if (Regex.IsMatch("A234456", @"[ABCEGHJKLMNPRSTVXY]\d{1}"))
temp = temp;
else
temp = temp;

Doesn't the {1} say only one digit?

It says to match one _character_. But it doesn't say that there cannot be
any other characters in the string.
 
Peter Duniho said:
Why does this work?

if (Regex.IsMatch("A234456", @"[ABCEGHJKLMNPRSTVXY]\d{1}"))
temp = temp;
else
temp = temp;

Doesn't the {1} say only one digit?

It says to match one _character_. But it doesn't say that there cannot be
any other characters in the string.

But then how would I say to stop at the 1 numeric digit so that we only have
2 characters in the string, one alpha and one numeric?

Thanks,

Tom
 
But then how would I say to stop at the 1 numeric digit so that we only
have
2 characters in the string, one alpha and one numeric?

Include a pattern in your regex that matches one numeric character after
one letter character.

Note that that will still not prevent there also being other characters in
the string. But you haven't yet stated that as a requirement.

If and when you are willing to provide a clear, specific, unambiguous
specification of what your regex expression should do, then someone
(probably not me, as I'm not really a regex expert) may be able to provide
an exact regex expression that suits your needs.

Until then, we are pretty much going to be left answering your questions
with "that's how the regex behaves because that's how it'd documented to
behave", and "if you want your regex to do X, then you have to write the
regex to do X".

Pete
 
Peter Duniho said:
Include a pattern in your regex that matches one numeric character after
one letter character.

I thought that was what I was asking:

1 alpha
1 numeric

Only allow 2 characters in the string
Stop after the numeric - don't allow any more characters.

A5
C3
D7

But not

D75
D
1A
C22
C2A

etc.

Thanks,

Tom
 
I thought that was what I was asking:

1 alpha
1 numeric

Only allow 2 characters in the string
Stop after the numeric - don't allow any more characters.

"Stop after" is not the same as "don't allow".
A5
C3
D7

But not

D75
D
1A
C22
C2A

It sounds like the only thing missing from your initial attempt is to
implement the "don't allow". You already had the "stop after", but what
you really want is to require that there be no more characters between the
two that are matched and the end of the string. I believe that by
including '$' at the end of the regex pattern, that would accomplish
that. That would indicate that the end of the line (which if I recall
correctly also matches the end of the string) must immediately follow the
matched characters.

I take as granted that you have studied the problem carefully and have a
very good reason for using something as heavyweight as regex for what
appears to be something that could easily be addressed simply in code.

Pete
 
1 alpha
1 numeric

Only allow 2 characters in the string
Stop after the numeric - don't allow any more characters.

A5
C3
D7

But not

D75
D
1A
C22
C2A

etc.

According to your regular expression, it does a little more than just "1
alpha and 1 numeric"...

So, did you want alpha as in all alpha characters (including upper and
lowercase) or, from the sample data you provided AND the original expression
you posted, did you only want 1 upper case character and 1 number? The
following will perform the latter:

const string PATTERN = @"^[A-Z][0-9]$";
Match match;

match = Regex.Match("Z5", PATTERN); // Success
match = Regex.Match("a5", PATTERN); // Fail
match = Regex.Match("D75", PATTERN); // Fail
match = Regex.Match("C2A", PATTERN); // Fail

HTH,
Mythran
 
Back
Top