Regex to find space at or before the n-th character of a string

Viewed 7018

I need to find the space character at or before the nth character of a string.

Example, assuming that n is 20: in the string

 Find one space in the right place
^         ^       ^ ^
0         10      M 20

the h is in the 20th position, int this case I need to find the first space before the 20th position, the one before the word "the" in 18th position, just over the M.

I can find the nth character using positive lookbehinds like

(?<=.{80}).

but then I need to go back until I find the first space character and I don't know how.

I'm using notepad++ which doesn't support variable length lookbehinds, if possible I'd like an answer allowing me to use the regex also in notepad++. Thanks.

2 Answers
Related