Regex issue with special characters lookahead and lookbehind

Hi Team,
I am facing issue in regex.
My requirement is to capture word before and after “by”. For that i used below regex present in screenshot
But If you see the last line in Test string it is also having “By” word but its not highlighting word which are present before and after “by” because its including special character after “2020”
I need such regex which can accept any value (number,word,special character) after and before “by”
pls help in this to capture special character also after and before “by” word

Regards,
Sushant

image

1 Like

Hi @sshitol check it out

(.{10}?)by(?<=by)[^/]{10}

Cheers

1 Like

@vikaskulhari,

I want 10 words. I tried your solution it giving me 10 characters after and before “by”

1 Like

Hello @sshitol
try this
(?>\w+){1}(?>\sby)(?>\s\w+){1}
Capture

1 Like

Hi @vickydas,
The regex which u gave doesnt work with special character.
See the last line in screenshot.It also containing by word but it not highlighting forward and backward word. Becuase after 2020 there is special character.
please give solution on this
image

1 Like

Hello @sshitol
(?>\w+){1}(?>\sby)(?>\s\w+){1}| (?>\w+){1}(?>\sby)(?>\s\w+.){1}
Try this.This will check both the condition

1 Like

HI @vickydas,
what if i try to capture 2 or 3 word ahead and behind from “by”.
The number which we are passing in {} is dynamic means sometimes we need to capture 2 or sometimes 6 depend on users. In such case its not working
I change {} values from 1 to 3(see in screenshot). But it fails to capture 3 forward and backward from “by”
And also as I said, it should work for all special character because u did it for . only.
pls help me to resolve this
image

1 Like

Hello @sshitol
Try this pattern \S(?:\s\S+){0} by \S(?:\s\S+){0}**


Please note here the count starts from 0