2

I have a string in following format..

"ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC 11.00|ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC11.00|ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC 11.00";

What I am trying to do is find the next group which starts with pipe but is not followed by a - So the above string will point to 3 sections such as

ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC 11.00
ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC 11.00
ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC 11.00

I played around with following code but it doesn't seem to do anything, it is not giving me the position of the next block where pipe char is which is not followed by a dash (-)

String pattern = @"^+|[A-Z][A-Z][A-Z]$";

In the above my logic is

1:Start from the beginning
2:Find a pipe character which is not followed by a dash char
3:Return its position
4:Which I will eventually use to substring the blocks
5:And do this till the end of the string

Pls be kind as I have no idea how regex works, I am just making an attempt to use it. Thanks, language is C#

2 Answers 2

1

You can use the Regex.Split method with a pattern of \|(?!-).

Notice that you need to escape the | character since it's a metacharacter in regex that is used for alternatation. The (?!-) is a negative look-ahead that will stop matching when a dash is encountered after the | character.

var pattern = @"\|(?!-)";
var results = Regex.Split(input, pattern);
foreach (var match in results) {
    Console.WriteLine(match);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot...:-), however Can you please elaborate on what exactly each characters significance is in the expression \|(?!-)? I can understand first char is escape char for pipe,brackets are for group or block ? means 0 or more occurrences, but what does !- mean...in this context
@user1063108 I used a look-around assertion, which has a special syntax, so there's a difference from the other stuff. (...) is for grouping, and ? means optional (0 or 1). Look-arounds can be positive (?=...) or negative (?!...), and can also be used to look ahead or behind. (?!...) is a negative look-ahead, and inside I used the dash character, so altogether (?!-) is a negative look-ahead that will assert that a dash is not matched. \|(?!-) means match a pipe character that isn't followed by a dash. Learn more here.
0

My Regex logic for this was:

  • the delimiter is pipe "[|]"
  • we will gather a series of characters that are not our delimiter "(" not our delimiter ")" but at least one character "+"
  • "[^|]" is not our delimiter
  • "[|][-]" is also not our delimiter

Variable "pattern" could use a "*" instead of "+" if empty segments are acceptable. The pattern ends with a "?" since our final string segment (in your example) does not have a pipe character.

using System;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace ConsoleTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = "ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC 11.00|ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC11.00|ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC 11.00";
            var pattern = "([^|]|[|][-])+[|]?";
            Match m;

            m = Regex.Match(input, pattern);
            while (m.Success) {
                Debug.WriteLine(String.Format("Match from {0} for {1} characters", m.Index, m.Length));
                Debug.WriteLine(input.Substring(m.Index, m.Length));
                m = m.NextMatch();
            }
        }
    }
}

Output is:

Match from 0 for 50 characters
ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC 11.00|
Match from 50 for 49 characters
ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC11.00|
Match from 99 for 49 characters
ABC 12.23-22-22-11|-ABC 33.20-ABC 44.00-ABC 11.00  

2 Comments

Thanks for your detailed response, however I had already selected above answer as my answer....sorry...Thanks again
Not a problem, I only provided so much detail because the first answer did not have code when I added mine. It was good practice, anyway as I usually write regex code in Perl! :)

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.