0

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?

10
  • That's one example, but can you describe in words generally what you want your regex to match and what you don't want it to match? What characterizes the "section 2" in "section 2(14)" versus "section 2." at the end that indicates it should not be matched? Commented Oct 24, 2015 at 18:15
  • @lurker It matching section 2 from section 2(14), it should not match section from section 2(14) Commented Oct 24, 2015 at 18:19
  • Yes, I understand that. By why? What is it about "section 2(14)" that makes you not want to match it? You need to have some specification/rule you can describe for matching in order to devise a regex. Commented Oct 24, 2015 at 18:20
  • So, you want it to match anything that contains the search string, and is followed only by a space or a dot? Commented Oct 24, 2015 at 18:21
  • @lurker yes, I don't want to match it in section 2(14) Commented Oct 24, 2015 at 18:21

2 Answers 2

2

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 (.

Sign up to request clarification or add additional context in comments.

1 Comment

[ |.|,|;|?|!] = [ .,;?|!]. Character classes are not groups.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.