Adam
Adam

Reputation: 3665

Regex to match css class name when multiple used

I am trying to make a regex to find any element that has a specific class.

For example

<span class = "float">

For which I came up with this:

\s*class\s*=\s*('|")\s*float\s*('|")\s*

But then of course you can have multiple CSS declared

<span class = "float align cssnames">

I am not sure how you would make everything else optional.

Upvotes: 1

Views: 1063

Answers (3)

evanmcdonnal
evanmcdonnal

Reputation: 48086

Regex rgx = new Regex("\"float.*\"");

Will match "float anything here but ends with the closing quote"

Upvotes: 0

ΩmegaMan
ΩmegaMan

Reputation: 31616

This code will look for the class=" and " as a non matched anchor. Then it will take each of the attribute values found and put each of them into a named capture group called Assignements.

Then linq will extract out the just those named capture(s) and then look for a single "Float" item from the captured group and return true or false.

string data = @"<span class = ""float align cssnames"">";

string pattern = @"(?:class\s*=\s*\x22)((?<Assignments>[^\s\x22]+)(?:\s?))+(?:\x22)";

var containsFloat =
   Regex.Matches(data, pattern, RegexOptions.Multiline)
      .OfType<Match>()
      .Select(mt => mt.Groups["Assignments"].Captures
                                             .OfType<Capture>()
                                             .Select(c => c.Value))
      .SelectMany(assignments => assignments) // is an IEnumerable<string> = { "float", "align", "cssnames" } at this point
      .Any(assignment => assignment == "float"); // True!

Upvotes: 0

Rawling
Rawling

Reputation: 50114

Maybe \s*class\s*=\s*('|")[\w\s]*\bfloat\b[\w\s]*('|")\s*?

In between the quotes it looks for float with a word boundary on either side, possibly surrounded by further word characters and/or spaces (i.e. other CSS classes).

Upvotes: 3

Related Questions