Deepti Kakade
Deepti Kakade

Reputation: 3213

Replace a exact string using Regex in ruby

Consider the the following text

The capital asset as defined in section 2(14) is an exhaustive definition which encompasses all properties of any kind with certain exceptions but the key word is that the property should be "held" section 2.

Now I want to find section 2, for the same I have written the following Regex:

/\bsection+\.*\s+2\b/i

But it is also matching section 2 of section 2(14). I just want it to only match the exact text, not the part of text which is matching with the regex. I know I need to modify the regex, but what are the changes required?

Upvotes: 0

Views: 116

Answers (2)

JuanBoca
JuanBoca

Reputation: 724

Try with \bsection+\.*\s+2([ .,;?|!])/i . This will only match with section 2 if it is followed by a space or a punctuation mark different than (.

Upvotes: 2

ekhaliki
ekhaliki

Reputation: 54

/\bsection+.*\s+2\b([[:punct:]]|\s/

basically you would want the word to end with whitespace or a punctuation.

Upvotes: 0

Related Questions