regular expression

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

Hi,
reg expressions are very handy but it's not my speciality. Can someone help
me constructing one for the next problem?
PxxyyPxxyyPxxyyy
Thats the string I have to split. The Pxx is always constructed like that, a
'P' and 2 numbers or letters.
The yy can be 1 or more numbers or letters.
The xx's and yy's belong to a group combined with the 'P' and I need both of
the values.
I guess I have to loop through a matchCollection.
Thanks for your help.
Frank
 
Hi Frank,

I recently had a fight with regular expressions, too, but even though I
finally achieved my goal I have to admit it cost me so much time that it
would be more efficient to just do it the old good way...

that would look in your case something like this:

string[] tokens = yourString.Split('P');
for (i=0; tokens.Length; i++)
{
xx=tokens.Substring(0,2);
yy=tokens.Substring(2);
// do something with xx and yy
}

HTH,
Stefan
 
Hi,
reg expressions are very handy but it's not my speciality. Can
someone help me constructing one for the next problem?
PxxyyPxxyyPxxyyy
Thats the string I have to split. The Pxx is always constructed
like that, a 'P' and 2 numbers or letters.
The yy can be 1 or more numbers or letters.
The xx's and yy's belong to a group combined with the 'P' and I
need both of the values.
I guess I have to loop through a matchCollection.

Frank,

MatchCollection mc = Regex.Matches("PxxyyPxxyyPxxyyy", "(?:P)(?<x>.{2})(?<y>[^P]+)");
foreach (Match m in mc)
{
Console.WriteLine(m.Groups["x"].ToString());
Console.WriteLine(m.Groups["y"].ToString());
}
 
Try this :

Regex regex=new
Regex("P(?<x>[A-Za-z0-9]{2})(?<y>(?([^P])[A-Za-z0-9]|)*)");
MatchCollection mathes=regex.Matches(textBox1.Text);
foreach(Match match in mathes) {
MessageBox.Show("xx="+match.Groups["x"].Value+" ,
yy="+match.Groups["y"].Value);
}

Hope it helps.

Ludovic SOEUR.
 
string[] parts = String.Split('P');

walk the array and trivially parse xx = parts.Substring(0,2) and yy =
parts.Substring(2)
 
Thanks Peter, but I could have thought about this solution myself.
I WANT to use a regular expression. It's much more flexible.
Frank

Peter Wone said:
Oh damn I should have kept reading :)

Stefan L said:
Hi Frank,

I recently had a fight with regular expressions, too, but even though I
finally achieved my goal I have to admit it cost me so much time that it
would be more efficient to just do it the old good way...

that would look in your case something like this:

string[] tokens = yourString.Split('P');
for (i=0; tokens.Length; i++)
{
xx=tokens.Substring(0,2);
yy=tokens.Substring(2);
// do something with xx and yy
}

 
Use this regex : "P(?<x>[A-Za-z0-9]{2})(?<y>[A-OQ-Za-z0-9]+)"

Following lines illustrate how to access to match collection :

Regex regex=new Regex("P(?<x>[A-Za-z0-9]{2})(?<y>[A-OQ-Za-z0-9]+)");
MatchCollection mathes=regex.Matches(textBox1.Text);
foreach(Match match in mathes) {
MessageBox.Show("xx="+match.Groups["x"].Value+" ,
yy="+match.Groups["y"].Value);
}

Hope it helps.

Ludovic SOEUR.


Frank said:
Thanks Peter, but I could have thought about this solution myself.
I WANT to use a regular expression. It's much more flexible.
Frank

Peter Wone said:
Oh damn I should have kept reading :)

Stefan L said:
Hi Frank,

I recently had a fight with regular expressions, too, but even though I
finally achieved my goal I have to admit it cost me so much time that it
would be more efficient to just do it the old good way...

that would look in your case something like this:

string[] tokens = yourString.Split('P');
for (i=0; tokens.Length; i++)
{
xx=tokens.Substring(0,2);
yy=tokens.Substring(2);
// do something with xx and yy
}


 
Thanks, works like a charm.
Frank
Ludovic SOEUR said:
Use this regex : "P(?<x>[A-Za-z0-9]{2})(?<y>[A-OQ-Za-z0-9]+)"

Following lines illustrate how to access to match collection :

Regex regex=new Regex("P(?<x>[A-Za-z0-9]{2})(?<y>[A-OQ-Za-z0-9]+)");
MatchCollection mathes=regex.Matches(textBox1.Text);
foreach(Match match in mathes) {
MessageBox.Show("xx="+match.Groups["x"].Value+" ,
yy="+match.Groups["y"].Value);
}

Hope it helps.

Ludovic SOEUR.


Frank said:
Thanks Peter, but I could have thought about this solution myself.
I WANT to use a regular expression. It's much more flexible.
Frank

Peter Wone said:
Oh damn I should have kept reading :)

Hi Frank,

I recently had a fight with regular expressions, too, but even though
I
finally achieved my goal I have to admit it cost me so much time that it
would be more efficient to just do it the old good way...

that would look in your case something like this:

string[] tokens = yourString.Split('P');
for (i=0; tokens.Length; i++)
{
xx=tokens.Substring(0,2);
yy=tokens.Substring(2);
// do something with xx and yy
}


 
Back
Top