Regex: Find words between words
-
I have
a gift formy mother. I wanta nice book formyself. I lovea beautiful woman forher eyes.So, as you can see, I want to find all words expression starting with
aand ending withfor. But for not more then 6 words between them.I made a regex, but it is not so good.
\bA[\s\S]*?\w+{1,6}\bFOR[\s\S]*? -
First, may I say, thank you for showing example data and what you had previously tried. That make it so much easier to help you.
I’ve got a solution that I think will work:
(?i)\bA\b(?:\s*\b\w+\b\s*){1,6}\bFOR\bSee it described at https://regexr.com/3uuht.In your sequence, the problems I see:
[\s\S]*?means a non-greedy selection of 0 or more spaces or non-spaces – that means it non-greedy matches anything. I am not sure that’s what you really intended\w+{1,6}: one or more word characters (from\w+), followed by nothing repeated 1 to 6 times. This is actually an error in the regex, and probably will cause the regex to do nothing (NPP Find says “invalid regular expression”). If you want the{1,6}to apply to the groupings of one-or-more word characters, you have to parenthesize around the\w+, as shown
I fixed those in my version.
I also added a couple more word boundaries, just to be explicit, and to prevent some false matches that I think may or may not be what you want.
Caveats in my interpretation:
(?i): I was explicit about case insensitive (otherwiseAandFORwould not matchaandfor)- I wanted to make it match
a day for, but notan evening for, because you said “a”. If you want it to be able to start withaoran, but notand, use\bAN*\b - Since you said “for”, I assumed you wanted to match
a day for, but nota day to go forthnora day to go forward. If you want the latter as well, then\bFOR\w*\b - You weren’t explict as to whether you wanted the space after “for” to be included in the match or not (your final [\s\S] kindof hinted you do, but I wasn’t sure). If you do, then
\bFOR\b\s+
-
Hello @vasile-caraus, @peterjones and All,
Why not this regex :
a\s+(\w+\s+){1,6}?forOR
a\h+(\w+\h+){1,6}?forWith the sample text, below, it matches, only, in sentences
2to7!1 : If was a for sale ( Incorrect sentence ! ) 2 : It was a house for sale. 3 : It was a small house for sale. 4 : It was a small old house for sale. 5 : It was a very small old house for sale. 6 : It was a very small old green house for sale. 7 : It was a very small old green wooden house for sale. 8 : It was a very small old green wooden house designed for sale. 9 : It was a very small old green wooden house not designed for sale.With a single long line of text, joining lines
1to9, it works nice too !If was a for sale ( Incorrect sentence ! ) It was a house for sale. It was a small house for sale. It was a small old house for sale. It wasavery small old house for sale. It was a very small old green house for sale. It was a very small old green wooden house for sale. It was a very small old green wooden house designed for sale. It was a very small old green wooden house not designed for sale.Best Regards,
guy038
-
thank you
also,
a(\W+\w+){1,6}\W+for
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login