sday03
sday03

Reputation: 33

Regex to match a 28 character ID string with special characters

I have a little app that reads in some log files made by another application. In these files lines similar to this are being processed:

 ban added reason='Posting links to malware websites' cluid='oNNtrNGo6kdxNRshT8MiHlq4wR8=' bantime=0 by client 'Someone'(id:4)

Currently I have a little bit of Regex \w{27}= that will get the cluid value in that string. The cluids are always 27 characters long with an '=' at the end. However there are some of those ID's that have special characters within the ID itself, example: IVz0tUZThCdbBnCWjf+axoMqVTM= (notice the '+' character) This means that my regex does not match this ID.

What do I need to add to the regex in order for it to match both ID's?

Upvotes: 1

Views: 699

Answers (1)

Shar1er80
Shar1er80

Reputation: 9041

You're interested in only cluid's value (that's in between single quotes). You can try this pattern:

"cluid='([^']{27}=)'"

it captures 27 characters that are not a single quote (assuming a single quote cannot be part of the value) followed by the equal sign into capture group 1.

Example:

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string line1 = "ban added reason='Posting links to malware websites' cluid='oNNtrNGo6kdxNRshT8MiHlq4wR8=' bantime=0 by client 'Someone'(id:4)";
        Match m = Regex.Match(line1, "cluid='([^']{27}=)'");
        if (m.Success)
        {
            Console.WriteLine(m.Groups[1]);
        }

        string line2 = "ban added reason='Posting links to malware websites' cluid='IVz0tUZThCdbBnCWjf+axoMqVTM=' bantime=0 by client 'Someone'(id:4)";
        m = Regex.Match(line2, "cluid='([^']{27}=)'");
        if (m.Success)
        {
            Console.WriteLine(m.Groups[1]);
        }
    }
}

Results:

oNNtrNGo6kdxNRshT8MiHlq4wR8=
IVz0tUZThCdbBnCWjf+axoMqVTM=

Fiddle Demo

Upvotes: 1

Related Questions