trim spaces before evaluating regex

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

What's the proper way to write a validation regex so that all leading and
trailing spaces are trimmed before evaluation ? Thanks.
 
John said:
What's the proper way to write a validation regex so that all leading
and trailing spaces are trimmed before evaluation ? Thanks.

You could extend that regex, so that leading/trailing spaces are accepted.
Don't forget to trim the value then before you use it.
[ ]*<original RE>[ ]*

Hans Kesting
 
John said:
What's the proper way to write a validation regex so that all leading
and trailing spaces are trimmed before evaluation ? Thanks.

You could extend that regex, so that leading/trailing spaces are accepted.
Don't forget to trim the value then before you use it.
[ ]*<original RE>[ ]*

Hans Kesting

Almost a correct answer, but you forgot the symbols for matching the
complete string.

^[ ]*<original RE>[ ]*$

Also, if you want to remove tabs and other whitespace characters you
can also use \s.

^\s*<original RE>\s*$
 
Marcus Andrén said:
On Thu, 29 Sep 2005 11:23:30 +0200, "Hans Kesting"
Almost a correct answer, but you forgot the symbols for matching the
complete string.

^[ ]*<original RE>[ ]*$

That's not true, if it's for use in RegularExpressionValidator (as i suppose
is true for the OP).
RegularExpressionValidator checks, if the whole value matches the RE, not if
it contains a match.
(Internally it adds ^ and $ IMHO.

Christof
 
^[ ]*<original RE>[ ]*$

That's not true, if it's for use in RegularExpressionValidator (as i suppose
is true for the OP).
RegularExpressionValidator checks, if the whole value matches the RE, not if
it contains a match.
(Internally it adds ^ and $ IMHO.

Christof

Oops. That is what I get for assuming that the validator took a true
regular expression instead of a deformed one. :)

Marcus
 
In this format:

[ ]*<original RE>[ ]*

when I move the cursor over it , I am finding a what appears to be a space
character between the [].

Is that how a regex works? It can identify a single space as a character to
be matched ?




Marcus Andrén said:
^[ ]*<original RE>[ ]*$

That's not true, if it's for use in RegularExpressionValidator (as i
suppose
is true for the OP).
RegularExpressionValidator checks, if the whole value matches the RE, not
if
it contains a match.
(Internally it adds ^ and $ IMHO.

Christof

Oops. That is what I get for assuming that the validator took a true
regular expression instead of a deformed one. :)

Marcus
 
Also, in the form :

\s*<original RE>\s*

why is it possible to not include the [] ?

In other words, why is it that the form does not have to be:

[\s]*<original RE>[\s]*

Thanks.


Marcus Andrén said:
^[ ]*<original RE>[ ]*$

That's not true, if it's for use in RegularExpressionValidator (as i
suppose
is true for the OP).
RegularExpressionValidator checks, if the whole value matches the RE, not
if
it contains a match.
(Internally it adds ^ and $ IMHO.

Christof

Oops. That is what I get for assuming that the validator took a true
regular expression instead of a deformed one. :)

Marcus
 
Also, in the form :

\s*<original RE>\s*

why is it possible to not include the [] ?

In other words, why is it that the form does not have to be:

[\s]*<original RE>[\s]*

Thanks.

Actually the [] clammers aren't needed. They were probably used to
clearly demonstrate taht it was a space.

[] is a construct to match multiple types of characters.
[abc] for example matches any of the characters a, b and c.
[abc]* can be used to match any string containing only a,b and c's.

Since space is a single character it could just as easily have been
used without []. " *<original RE> *" would work just fine.

While \s represents all whitespace characters it is still treated as a
single character in the regular expressions syntax so it can also be
written without []. If however you would like to match any whitespace
character or the character b or c, you would have to use [bc\s].

If you want a brief description of all regular expression elements you
can use the following link

http://msdn.microsoft.com/library/d...l/cpconregularexpressionslanguageelements.asp

A good and quite simple tutorial that I used once upon a time is here

http://gnosis.cx/publish/programming/regular_expressions.html


Finally, A minor advice. If you ever use regular expressions inside
your c# code, prepend @ to your string. @ tells the compiler that the
\ character shouldn't be treated as a special character. This is
useful when working with strings that contain the character \, like
regular expressions or file locations. For example:

"c:\\program files\\myprogram\\program.exe"
can also be written
@"c:\program files\myprogram\program.exe"
in c#.
 
John said:
In this format:

[ ]*<original RE>[ ]*

when I move the cursor over it , I am finding a what appears to be a space
character between the [].

Is that how a regex works? It can identify a single space as a character
to be matched ?

In addition to what Marcus said, be careful when using spaces or other
whitespace in regular expressions. There's a flag in the RegexOptions
enumeration that can be passed in to various methods of the Regex class,
it's called IgnorePatternWhitespace. If this flag is set when the
expression is used, any whitespace in the pattern is not assumed to be
part of the match. Obviously, if you call the Regex method yourself,
you're bound to know whether you're passing that flag or not. But when the
call is made from other code than your own, or you're using a regular
expression tester, it's important to know this when working with
expressions that want to match whitespace.


Oliver Sturm
 
Back
Top