Regex repeating capture

  • Thread starter Thread starter jaylucier
  • Start date Start date
J

jaylucier

Howdy,

I'm trying to break an input string into multpile pieces using a
series of delimiters that start with an asterisk. Following the
asterisk is a mulitple character identifier immediately followed by a
data string of variable length. The input string may contain more than
one identifier anywhere in the string.

Here is an example:
*CZ1 2.3 4-56 *fuuuS24364 08 23 72

I'd like to break this into
CZ
1 2.3 4-56
fuuu
S24364 08 23 72

I have tried the pattern (?:\*(CZ|fuuu)(.*)), which produces the
following ouput:
CZ
1 2.3 4-56 *fuuuS24364 08 23 72

How can I force it to repeat the capturing?

Thanks,
Jay
 
Use:

public string[] Split (
params char[] separator
)
to split your string on the asterisk as a first step.

Now you can enumerate over the string array splitting out your identifiers
and data strings. You could use a StringBuilder to build what ever you want
to output.

Now you can use:

public bool StartsWith (
string value
)andpublic string Substring (
int startIndex
)e.g.StringBuilder sb = new StringBuilder();
foreach (string s in strArray)
{
if (s.StartsWith("CZ")
{
sb.Append("CZ");
sb.Append(s.Substring(2));
}
else
{
sb.Append("fuuu");
sb.Append(s.Substring(4))
}
}

return sb.ToString();

I'm sure there's an easier way using a Regex, but I can't be bothered to
puzzle it out.

HTH


Peter
 
Sorry, this was a simple example. In all, there are 50+ identifiers
and * is allowed in that data as long it isn't immediately followed by
an identifier, otherwise it is considered another identifier.

Use:

public string[] Split (
params char[] separator
)
to split your string on the asterisk as a first step.

Now you can enumerate over the string array splitting out your identifiers
and data strings. You could use a StringBuilder to build what ever you want
to output.

Now you can use:

public bool StartsWith (
string value
)andpublic string Substring (
int startIndex
)e.g.StringBuilder sb = new StringBuilder();
foreach (string s in strArray)
{
if (s.StartsWith("CZ")
{
sb.Append("CZ");
sb.Append(s.Substring(2));
}
else
{
sb.Append("fuuu");
sb.Append(s.Substring(4))
}

}return sb.ToString();

I'm sure there's an easier way using a Regex, but I can't be bothered to
puzzle it out.

HTH

Peter

I'm trying to break an input string into multpile pieces using a
series of delimiters that start with an asterisk. Following the
asterisk is a mulitple character identifier immediately followed by a
data string of variable length. The input string may contain more than
one identifier anywhere in the string.
Here is an example:
*CZ1 2.3 4-56 *fuuuS24364 08 23 72
I'd like to break this into
CZ
1 2.3 4-56
fuuu
S24364 08 23 72
I have tried the pattern (?:\*(CZ|fuuu)(.*)), which produces the
following ouput:
CZ
1 2.3 4-56 *fuuuS24364 08 23 72
How can I force it to repeat the capturing?
Thanks,
Jay
 
Howdy,

I'm trying to break an input string into multpile pieces using a
series of delimiters that start with an asterisk. Following the
asterisk is a mulitple character identifier immediately followed by a
data string of variable length. The input string may contain more than
one identifier anywhere in the string.

Here is an example:
*CZ1 2.3 4-56 *fuuuS24364 08 23 72

I'd like to break this into
CZ
1 2.3 4-56
fuuu
S24364 08 23 72

I have tried the pattern (?:\*(CZ|fuuu)(.*)), which produces the
following ouput:
CZ
1 2.3 4-56 *fuuuS24364 08 23 72

How can I force it to repeat the capturing?

Thanks,
Jay

So, to split based on an * using a regular expression:

string pattern = @"\*(?<Text>[^\*]+)";
string input = "*CZ1 2.3 4-56 *fuuuS24364 08 23 72";
Match match = Regex.Match(input, pattern);

while (match.Success) {
Console.WriteLine(match.Groups["Text"].Value);
match = match.NextMatch();
}

HTH,
Mythran
 
Mythran said:
Howdy,

I'm trying to break an input string into multpile pieces using a
series of delimiters that start with an asterisk. Following the
asterisk is a mulitple character identifier immediately followed by a
data string of variable length. The input string may contain more than
one identifier anywhere in the string.

Here is an example:
*CZ1 2.3 4-56 *fuuuS24364 08 23 72

I'd like to break this into
CZ
1 2.3 4-56
fuuu
S24364 08 23 72

I have tried the pattern (?:\*(CZ|fuuu)(.*)), which produces the
following ouput:
CZ
1 2.3 4-56 *fuuuS24364 08 23 72

How can I force it to repeat the capturing?

Thanks,
Jay

So, to split based on an * using a regular expression:

string pattern = @"\*(?<Text>[^\*]+)";
string input = "*CZ1 2.3 4-56 *fuuuS24364 08 23 72";
Match match = Regex.Match(input, pattern);

while (match.Success) {
Console.WriteLine(match.Groups["Text"].Value);
match = match.NextMatch();
}

HTH,
Mythran

ahh, I didn't know you wanted to break it out into identifier, text,
identifier, text...thus the previous post should be obliterated :P...do you
know if the identifier is always 4 characters? Hope so, the following
example shows how to achieve this:

string pattern = @"\*(?<Identifier>.{4})(?<Value>[^\*]+)";
string input = "*CZ1 2.3 4-56 *fuuuS24364 08 23 72";
Match match = Regex.Match(input, pattern);

while (match.Success) {
Console.WriteLine(
"Identifier: {0} - Value: {1}",
match.Groups["Identifier"].Value,
match.Groups["Value"].Value
);
match = match.NextMatch();
}

HTH,
Mythran
 
The identifier is at least 2 character, but has no upper limit.


Thanks,
Jay

So, to split based on an * using a regular expression:
string pattern = @"\*(?<Text>[^\*]+)";
string input = "*CZ1 2.3 4-56 *fuuuS24364 08 23 72";
Match match = Regex.Match(input, pattern);
while (match.Success) {
Console.WriteLine(match.Groups["Text"].Value);
match = match.NextMatch();
}
HTH,
Mythranahh, I didn't know you wanted to break it out into identifier, text,
identifier, text...thus the previous post should be obliterated :P...do you
know if the identifier is always 4 characters? Hope so, the following
example shows how to achieve this:

string pattern = @"\*(?<Identifier>.{4})(?<Value>[^\*]+)";
string input = "*CZ1 2.3 4-56 *fuuuS24364 08 23 72";
Match match = Regex.Match(input, pattern);

while (match.Success) {
Console.WriteLine(
"Identifier: {0} - Value: {1}",
match.Groups["Identifier"].Value,
match.Groups["Value"].Value
);
match = match.NextMatch();

}HTH,
Mythran
 
I know what the identifiers are, so I'm okay with replacing the .{4}
with (Identifier1|Identifier2|...|IdentifierN) at run time. However, I
cannot blindly end the data capture on an asterisk. "*CZ1 2.3 4*A56
*fuuuS24364 08 23 72" is also valid provide *A6 is not a valid
identifier. The data capture can only end if it encounters another
valid identifier.

The identifier is at least 2 character, but has no upper limit.

Thanks,
Jay

Howdy,
I'm trying to break an input string into multpile pieces using a
series of delimiters that start with an asterisk. Following the
asterisk is a mulitple character identifier immediately followed by a
data string of variable length. The input string may contain more than
one identifier anywhere in the string.
Here is an example:
*CZ1 2.3 4-56 *fuuuS24364 08 23 72
I'd like to break this into
CZ
1 2.3 4-56
fuuu
S24364 08 23 72
I have tried the pattern (?:\*(CZ|fuuu)(.*)), which produces the
following ouput:
CZ
1 2.3 4-56 *fuuuS24364 08 23 72
How can I force it to repeat the capturing?
Thanks,
Jay
So, to split based on an * using a regular expression:
string pattern = @"\*(?<Text>[^\*]+)";
string input = "*CZ1 2.3 4-56 *fuuuS24364 08 23 72";
Match match = Regex.Match(input, pattern);
while (match.Success) {
Console.WriteLine(match.Groups["Text"].Value);
match = match.NextMatch();
}
HTH,
Mythranahh, I didn't know you wanted to break it out into identifier, text,
identifier, text...thus the previous post should be obliterated :P...do you
know if the identifier is always 4 characters? Hope so, the following
example shows how to achieve this:
string pattern = @"\*(?<Identifier>.{4})(?<Value>[^\*]+)";
string input = "*CZ1 2.3 4-56 *fuuuS24364 08 23 72";
Match match = Regex.Match(input, pattern);
while (match.Success) {
Console.WriteLine(
"Identifier: {0} - Value: {1}",
match.Groups["Identifier"].Value,
match.Groups["Value"].Value
);
match = match.NextMatch();
}HTH,
Mythran
 
Jay said:
I know what the identifiers are, so I'm okay with replacing the .{4}
with (Identifier1|Identifier2|...|IdentifierN) at run time. However, I
cannot blindly end the data capture on an asterisk. "*CZ1 2.3 4*A56
*fuuuS24364 08 23 72" is also valid provide *A6 is not a valid
identifier. The data capture can only end if it encounters another
valid identifier.

The identifier is at least 2 character, but has no upper limit.

Thanks,
Jay

I'm trying to break an input string into multpile pieces using a
series of delimiters that start with an asterisk. Following the
asterisk is a mulitple character identifier immediately followed by
a
data string of variable length. The input string may contain more
than
one identifier anywhere in the string.
Here is an example:
*CZ1 2.3 4-56 *fuuuS24364 08 23 72
I'd like to break this into
CZ
1 2.3 4-56
fuuu
S24364 08 23 72
I have tried the pattern (?:\*(CZ|fuuu)(.*)), which produces the
following ouput:
CZ
1 2.3 4-56 *fuuuS24364 08 23 72
How can I force it to repeat the capturing?

So, to split based on an * using a regular expression:
string pattern = @"\*(?<Text>[^\*]+)";
string input = "*CZ1 2.3 4-56 *fuuuS24364 08 23 72";
Match match = Regex.Match(input, pattern);
while (match.Success) {
Console.WriteLine(match.Groups["Text"].Value);
match = match.NextMatch();
}
HTH,
Mythranahh, I didn't know you wanted to break it out into identifier,
text,
identifier, text...thus the previous post should be obliterated :P...do
you
know if the identifier is always 4 characters? Hope so, the following
example shows how to achieve this:
string pattern = @"\*(?<Identifier>.{4})(?<Value>[^\*]+)";
string input = "*CZ1 2.3 4-56 *fuuuS24364 08 23 72";
Match match = Regex.Match(input, pattern);
while (match.Success) {
Console.WriteLine(
"Identifier: {0} - Value: {1}",
match.Groups["Identifier"].Value,
match.Groups["Value"].Value
);
match = match.NextMatch();
}HTH,
Mythran

How many identifiers are there? If there are a small list (say, less than
10ish), then you can use the regex OR character '|' in the pattern to
separate the list of valid identifiers instead of matching on the asterisk
itself.

HTH,
Mythran
 

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