1

I have a string:

string = %q{<span class="no">2503</span>read_attribute_before_type_cast(<span class="pc">self</span>.class.primary_key)}

In this example I want to match the words 'class' which are not in the tag. Regexp for this:

/\bclass[^=]/

But the problem is that it matches the last letter

/\bclass[^=]/.match(string) => 'class.'

I don't want have a last dot in a result. I've tried this regexp:

/\bclass(?:[^=])/

but still got the same result. How to limit the result to 'class'? Thanks

megas
  • 21,401
  • 12
  • 79
  • 130

3 Answers3

2

You are almost correct, but you have an error in your look ahead. Try this:

/\bclass(?!=)/

The regex term (?!=) means the input to the right must not match the character '='

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You can take your variable string and extract a subsection using groups:

substring = string[/\b(class)[^=]/, 1]

The brackets around class will set that as the first "group", which is referred to by the 1 as the second parameter in the square brackets.

robbrit
  • 17,560
  • 4
  • 48
  • 68
0

Assuming your only issue is keeping it from matching span.class.blah, just ignore . as well, so [^=.].

Sysyphus
  • 1,061
  • 5
  • 10