Take the 2-minute tour ×

I am trying to replace all the words except the first 3 words from the String (using textpad).

Ex value: This is the string for testing.

I want to extract just 3 words: This is the from above string and remove all other words.

I figured out the regex to match the 3 words (\w+\s+){3} but I need to match all other words except the first 3 words and remove other words. Can someone help me with it?

share|improve this question
What language are you using this in? If you can match the first 3 words to your satisfaction, discarding the original string & just replacing it with the content of your match seems more efficient. –  Wrikken Jan 25 at 2:52
This is kind of bodged together but it seems to work on phpliveregex.com/p/3jo. ^((\w+\s+){3})(.*)$. With this you get... everything, the first 3, the 3rd and then the rest... Not sure how you might use it from there though. Not sure how it would work for Textpad as I have no idea what it is. –  Qoop Jan 25 at 2:58
@Wrikken - the poster said textpad. Textpad states "Its powerful regular expression engine is compatible with Perl and JavaScript." –  bluefeet Jan 25 at 2:59
@bluefeet: exactly, the OP added 'using textpad' after I asked him what he used. Hence, successful comment, job done, I don't know textpad, so I bow out for the remainder ;) –  Wrikken Jan 25 at 3:04
@Wrikken: Oh my, sorry about that. Still getting a handle on answering questions on Stack Overflow. Thanks for explaining! –  bluefeet Jan 25 at 3:12

2 Answers 2

up vote 0 down vote accepted

Exactly how depends on the flavor, but to eliminate everything except the first three words, you can use:


which captures the first three words into capturing group 1, as well as the rest of the string. For your replace string, you use a reference to capturing group 1. In C# it might look like:

resultString = Regex.Replace(subjectString, @"^((?:\S+\s+){2}\S+).*", "${1}", RegexOptions.Multiline);
share|improve this answer

EDIT: Added the start-of-line anchor to each regex, and added TextPad specific flags.

If you want to eliminate the first three words, and capture the rest,


?: changes the first three words to a non-capturing group, and captures everything after it.

Is this what you're looking for? I'm not totally clear on your question, or your goal.

As suggested, here's the opposite. Capture the first three words only, and discard the rest:


Just move the ?: from the first to the second grouping.

As far as replacing that captured group, what do you want it replaced with? To replace each word individually, you'd have to capture each word individually:


And then, for instance, you could replace each with its first letter capitalized:

Replace with: \u$1 \u$2 \u$3

Result is This Is The

In TextPad, lowercase \u in the replacement means change only the next letter. Uppercase \U changes everything after it (until the next capitalization flag).

Try it:


(press on [Java] or whatever language is most relevant. Note that \u is not supported by RegexPlanet.)

share|improve this answer
I think he is trying to remove all words except the first 3. Maybe give him 2 solutions where the second solution is for this case. –  alvits Jan 25 at 2:54
Ok. Answer is updated. –  aliteralmind Mar 10 at 19:17

Your Answer


