Re: Escaping problem using Regular Expression

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Martin Marinov (memmarinov_at_mecrossroad.bg)
Date: 07/06/04


Date: Tue, 6 Jul 2004 09:32:31 +0300

Hi Henry,

in addition of Nick's answer, there is a little modification

@"`~\!@#\$\%\^\&\*\(\)_=\+\[\]\{\}\\\|;:',<\.>/\?" + "\""

Regards
Martin

"Nick Malik" <nickmalik@hotmail.nospam.com> wrote in message
news:OcKFc.21331$Oq2.9279@attbi_s52...
> Hi Henry,
> Regular expressions are made up of many of the characters that you placed
> into your string. Including the [ and ] characters. The pattern that you
> specify has an invalid part [] which is a pattern group where any member
can
> be matched, but it has no members. that makes the pattern invalid.
>
> Moving the ] to the front of the string means that the regex parser will
not
> see the bad pattern. However to be truly correct, you still need to quote
> any of the characters that can be interpreted as part of a pattern.
>
> @"`~\!@#\$\%\^\&\*\(\)_=+\[\]\{\}\\|;:',<.>/?" + "\""
>
> Hope this helps,
> --- Nick
>
> "Henry" <ignhenry@hotmail.com> wrote in message
> news:%23aBgmIeXEHA.3016@tk2msftngp13.phx.gbl...
> > I have this simple code,
> >
> > string escaped = Regex.Escape( @"`~!@#$%^&*()_=+[]{}\|;:',<.>/?" +
"\"" );
> > string input = @"a&+[b`${c}a'?sd:r]" + "\"" + @"@(-d)\e";
> > Regex re = new Regex( string.Format(@"([{0}]+)", escaped),
> > RegexOptions.CultureInvariant );
> > string s = re.Replace( input, "" );
> >
> > It doesn't seem to work, regular expression return without filter out
any
> > character
> > However if I remove or change the position of "]" to be the first
> character,
> > it works.
> >
> > string escaped = Regex.Escape( @"]`~!@#$%^&*()_=+[{}\|;:',<.>/?" +
"\"" );
> >
> > I totally do not understand how this regular expression escaping works.
> What
> > am I doing wrong here?
> >
> > Thanks
> > Henry
> >
> >
>
>



Relevant Pages

  • Re: RegExp irregularity in JScript
    ... of characters in the string is at ... All three strings match if the pattern is "."; ... the pattern as a submatch ") the entire string is returned, ... This looks like a bug in Microsoft's regular expression implementation (it ...
    (microsoft.public.scripting.jscript)
  • Parsing (X)HTML with regular expressions, or not (was: Help with a regular expression)
    ... I believe the principal reason why pre-written parsers are suggested ... and recommended instead of impromptu regular expression "one-liners" ... Unquoted attribute values may only contain name characters. ... HTML4.01, the pattern ...
    (comp.lang.php)
  • Re: Regular Expression Function
    ... I want a regular expression to compare sentences and then rate them as ... I have an array with a list of other phrases like so... ... characters will throw things off. ... "In an hour the system will go down for maintenance". ...
    (alt.php)
  • Re: Regular Expression Function
    ... I want a regular expression to compare sentences and then rate them as ... I have an array with a list of other phrases like so.. ... These will be stripped from the input first. ... characters will throw things off. ...
    (alt.php)
  • Re: Expert script (.bat) writers help needed (strip double-quote from string)
    ... Sets or returns the regular expression pattern being searched for. ... Always a RegExp object variable. ... May include any of the regular expression characters defined in the table in the Settings section. ...
    (microsoft.public.windowsxp.help_and_support)