Re: Regex repeating capture

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





"Mythran" <kip_potter@xxxxxxxxxxx> wrote in message news:406FDAFC-735E-433F-A47A-478A660F0679@xxxxxxxxxxxxxxxx


<jaylucier@xxxxxxxxx> wrote in message news:1170174574.488763.29890@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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


.



Relevant Pages

  • Re: Regex repeating capture
    ... series of delimiters that start with an asterisk. ... The input string may contain more than ... Match match = Regex.Match(input, pattern); ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex repeating capture
    ... The data capture can only end if it encounters another ... valid identifier. ... >>> series of delimiters that start with an asterisk. ... The input string may contain more>>> than ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Squeezing bytes out of a struct [long]
    ... int main(int argc, char *argv) ... const char *pattern; ... // encountered within the input string without falsely matching a true ... // input string character must be a voiced consonant ...
    (comp.arch.embedded)
  • Re: multiple-value-bind and cond
    ... a couple of different patterns in order to classify the input string. ... ;; acond = anaphoric cond ... (nth-value 1 (scan pattern input)))) ... (defmacro multiple-value-acond (&rest clauses) ...
    (comp.lang.lisp)
  • Re: multiple-value-bind and cond
    ... a couple of different patterns in order to classify the input string. ... ;; acond = anaphoric cond ... (nth-value 1 (scan pattern input)))) ... of similar form - otherwise the anaphoric cond would be a good idea. ...
    (comp.lang.lisp)