简体   繁体   中英

Extract groups of numbers inside brackets using RegEx

Suppose I have the following text block:

/HDMAreaCoverageValue CIP3BeginPrivate
/HDMZones <</HDMInkC [0.000000][0.000002][0.000400][0.006000]
/HDMInkM [0.006000][0.086000]
/HDMInkB [0.000000][0.000002][0.000400]
>> def

I'm trying to extract only the numbers inside the brackets - but , with my current pattern, all I get are curly braces as result. I'm currently trying:

Regex ColorValueRegex = new Regex("HDMZones([0-9,]{1,})+>>");

What is wrong with this pattern ? What I can see that I'm doing is extract numbers\\comma(or dot) from the data I'll provide later.

My code:

   foreach (var data in ValidFiles)
    {
        Regex ColorValueRegex = new Regex("HDMZones([0-9,]{1,})+>>");
        var RegexAux2 = ColorValueRegex.Match(data);
        //HDMZonesNumbers.Add(RegexAux2.Groups[1].ToString().Replace("HDMZones <</", "").Replace("<</", "").Replace("/", ""));
        string pattern = @"([\d]+[,.]{0,1})+";
        string NumbersCleaned = Regex.Match(RegexAux2.ToString(), pattern).Value;
    }

The commented part is because after extracting I'll add the resulting string into a list. I expect to extract a string like:

[0.000000][0.000002][0.000400][0.006000][0.006000][0.086000]

I already tried to use many examples provided from StackOverflow but none of 'em did what i need. Before writing this code, I had it working but was inside a mega foreach loop - now I'm treating data individually. Any help will be appreciated. Thanks

If you are only trying to match the number I suggest you use following regex.

Regex: \\[[\\d.]*\\]

Explanation:

  • It will match digits , dot and square brackets [] and return it as match.

Regex101 Demo

Try this in C#:

Regex ColorValueRegex = new Regex(@"\[[0-9\.]+\]");

That gives the answer you want.

How i achieved my result: i decided to split my requested pattern in two Regexes. One for just extracting numbers with some text on it, and other to get the numbers inside square brackets (the brackets come in). Then , later i'll replace [ with white space , and ] with a delimiter | that way i can split into an array and do some operations i'll need. Code follows:

public void ColorValueExtraction()//Processing all colors values
{
    StringBuilder sb = new StringBuilder(); //Creating a StringBuilder object to append later
    string ColorsWithBrackets = string.Empty;
    foreach (var data in ValidFiles) //data means a file - lots of text
    {

        Regex ValuesRegex = new Regex("HDMZones(.*)>>"); //Extracting only content between HDMZones and >>
        var RegexAux2 = ValuesRegex.Match(data); //Match my pattern on data
        ColorsWithBrackets = RegexAux2.Groups[1].ToString();
        ColorsWithBrackets = ColorsWithBrackets.Replace("HDMZones <</", "").Replace("<</", "").Replace("/", ""); //Replacing a few things

        var pattern = @"\[(.*?)\]"; //Extract numbers and brackets only
        var query = ColorsWithBrackets;

        var matches = Regex.Matches(query, pattern);
        foreach (Match m in matches) //Looping on matches ,numbers found
        {                   
            string aux2 = string.Empty; //auxiliar string 
            aux2 = m.Groups[1].ToString();//auxiliar string receives the match to string
            aux2 = Regex.Replace(aux2, @"\s+", "|");  //replacing all spaces with | , to be used as delimiter in other method               
            sb.Append(aux2); //each iteration appends to StringBuilder aux2 - so, in the end, sb will have all numbers from the respective file                                     
        }               
        HDMZonesNumbersLst.Add(sb.ToString()); //Adding each collection of numbers to a position in the list
    }           
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM