but I want to match only the first 3 char of words.
I WANt THE FIRst 3 CHAr OF WORds ONLy.
It’s for a rapid lector: only uppercase the begining of any words.
The best could be: (First 3 char)(Rest of the word or space)
Thank you !
Use positive lookahead (
(?=[pattern]) to match without including in the match.
appears to do what you want (if I’ve understood your spec correctly).
You can see it in action here.
New answer following clarification on spec
I think this does what you want:
The breakdown is:
- 1st capturing group:
Matches a maximum of 3 non-space characters (
\S used instead of
\w because I think you want to match characters with diacritics like
à and punctuation in the middle of words like
- 2nd capturing group:
Matches zero or more non-space characters (the remaining characters in each word) followed by one or more delimiter characters (space or period). I included period as a delimiter to match the last word. You might want to adjust that part depending on your exact needs.
See it in action here.