1

This Regex is a bit niche and I'm really struggling to get it down. Here is an example of what I hope it would do:

 Testing space was before this part word space after this part this part shouldn't count. 

I want to try and extract all of the text between either of the two groups of spaces, this is what I've tried so far:

 {2}.*?word.*? {2} 

(Not there is a space before both of the {2}'s)

It should only extract "space was before this part word space after this part". Any help would be appreciated, thanks!

4
  • 1
    Look at regex101.com/r/oOryYx/1, Testing is not part of the match. Commented Mar 27, 2020 at 0:06
  • If you add spaces before "Testing" it will count those in the Regex though. Sorry I should've clarified that above. Commented Mar 27, 2020 at 0:07
  • 2
    Then you need " {2}(?:(?! {2}).)*?word.*? {2}", see regex101.com/r/oOryYx/2 Commented Mar 27, 2020 at 0:08
  • That did it, thank you so much! Commented Mar 27, 2020 at 0:10

1 Answer 1

2

You may use

 {2}(?:(?! {2}).)*?word.*? {2} 

See the regex demo

Details

  • {2} - two spaces
  • (?:(?! {2}).)*? - any char other than line break chars, 0 or more times, but as few as possible, that does not start a two space character string
  • word - a word string
  • .*? - any 0+ chars other than line break chars, as few as possible
  • {2} - two spaces
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.