RegEx to get text within tags

后端 未结 5 1707
南旧
南旧 2021-02-06 18:51

I need a Regular Expressions to get the text within 2 tags.

Lets say I want an array returned containing any text within > and

相关标签:
5条回答
  • 2021-02-06 19:26

    This similar question has answers that will help:

    • Regex: To pull out a section a substing from a string between two tags
    0 讨论(0)
  • 2021-02-06 19:34

    Perl regexp would be

    $string =~ /color=rgb\((\d+),(\d+),(\d+)\)/;
    @array = ($1,$2,$3);
    

    But you probably need more information that this.

    0 讨论(0)
  • 2021-02-06 19:40

    I believe real problems will arise when you want to parse nesting constructs. For example, when you want to examine XML like this <data><data>123</data><data>456</data></data> to extract data included in outermost <data> tags one RegEx alone would not be enough. Just warn you to not use RegEx where some more (powerful and specific) methods exist. Real XML parsers should be considered when doing more complex tasks on XML. My 2 cents...

    0 讨论(0)
  • 2021-02-06 19:43

    Since you specifically mentioned C#, here's how I'm doing that exact parsing:

    private static readonly Regex RgbValuePattern = new Regex(@"(?<r>\d{1,3}) ?, ?(?<g>\d{1,3}) ?, ?(?<b>\d{1,3})",
                                                              RegexOptions.Compiled | RegexOptions.ExplicitCapture);
    

    Then later on...

    var match = RgbValuePattern.Match(value);
    
    if (match.Success)
    {
        int r = Int32.Parse(match.Groups["r"].Value, NumberFormatInfo.InvariantInfo);
        int g = Int32.Parse(match.Groups["g"].Value, NumberFormatInfo.InvariantInfo);
        int b = Int32.Parse(match.Groups["b"].Value, NumberFormatInfo.InvariantInfo);
        return Color.FromArgb(r, g, b);
    }
    
    0 讨论(0)
  • 2021-02-06 19:49

    Using Regex to parse XML is usually a really bad idea. See this answer.

    0 讨论(0)
提交回复
热议问题