1

Please give a suggestion here:

I try to do a regex in C#. Here is the text

E1A: pop(+)
call T
call E1
E1B: return
TA: call F
call T1

I want to split it like this:

1)
E1A: pop(+)
call T
call E1

2)
E1B: return

3)
TA: call F
call T1

I tought at lookbehind, but it's not working because of the .+

Here is what I hope to work but it doesn't:

"[A-Z0-9]+[:].+(?=([A-Z0-9]+[:]))"

Does anyone have a better ideea?

EDIT: The "E1A","E1B","TA" are changing. All it remains the same is that they are made by letter and numbers follow by ":"

5
  • Are "E1A", "E1B", and "TA" constants? Or do these values change, or do they move out of order? You haven't explicitly defined the rules for what you want to capture. Commented Jan 26, 2012 at 21:26
  • The "E1A","E1B","TA" are changing. All it remains the same is that they are made by letter and numbers follow by ":" Commented Jan 26, 2012 at 21:29
  • Are the line breaks consistent? Will you always have "\r\n<sequence of letters/numbers>:<sequence of letters/numbers spanning multiple lines>"? Commented Jan 26, 2012 at 21:30
  • In the original text I have line breaks. But I tought it would be easier if I take them out. That's why there is the .+(I replace them with "" before I make the regex).But if you have a solution for the \r\n included, would be great Commented Jan 26, 2012 at 21:32
  • I can make line breaks appear before every statement with a colon, but you can't do incrementing counters in Regex. Do you want it to count, or do you want it to just insert line breaks? Commented Jan 26, 2012 at 21:37

1 Answer 1

3
Regex regexObj = new Regex(
    @"^               # Start of line
    [A-Z0-9]+:        # Match identifier
    (?:               # Match...
     (?!^[A-Z0-9]+:)  #  (unless it's the start of the next identifier)
     .                #  ... any character,
    )*                #  repeat as needed.", 
    RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
allMatchResults = regexObj.Matches(subjectString);

Now allMatchResults.Count will contain the number of matches in subjectString, and allMatchResults.Item[i] will contain the ith match.

Sign up to request clarification or add additional context in comments.

2 Comments

it's working. Could you explain what it means? I mean I know that (?:...) matches the regex, but it's not returnd, but the rest? Thank you so much
:)) :)) exactly what I asked for. Thank's

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.