System.Text.RegularExpressions sintaxis

  • Thread starter Thread starter Juan
  • Start date Start date
J

Juan

Hi, I´m using System.Text.RegularExpressions, I want to find any acurrence
of the char & or % or |

Ehat is the correct sintax for the Match method? This is what I´m usinf but
is not working:
Match myMatch = Regex.Match(myString,"^[&|%]*$");

Thanks,

Juan.
 
try the following short program. it find any char & or % or |



class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

string myString = "1&2|3%4&5|6%";

Match m = Regex.Match(myString,@"[&\|%]");

int matchCount = 0;

while (m.Success)

{

Console.WriteLine("Match"+ (++matchCount) + "\t\t" + m.Value);

m = m.NextMatch();

}

Console.ReadLine();

}

}
 
Juan said:
Hi, I´m using System.Text.RegularExpressions, I want to find any acurrence
of the char & or % or |

Ehat is the correct sintax for the Match method? This is what I´m usinf but
is not working:
Match myMatch = Regex.Match(myString,"^[&|%]*$");

Thanks,

Juan.

The ^ and $ mean that the match should be the entire string (instead of
a substring).
The [..]* means that you will match zero or more occurrences of
the specified characters.
The [&|%] means that you will match "&", "|" and "%".

So: you will match only a string consisting entirely of &,| and/or %
characters (or an empty string).

To find any occurrence of one of those three characters, use
"[&|%]" as the RE.

Hans Kesting
 

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

Back
Top