spacegoing
spacegoing

Reputation: 5306

Regex: The word has maximum 30 characters but closest to 30 characters before it

I need a regex to express "The word has maximum 30 characters but closest to 30 characters before it".

For example, suppose i have the following sentence:

It is a dark time for the Rebellion. Although the 

The result of the expression should return "Rebellion".

Currently I use the expression:

(?<=.{30})\b\w+\b

But this will return "Although". Anybody can help me get out of this? Many Thanks!

Upvotes: 0

Views: 257

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

I think you want something like this,

^.{1,30}\b(\w+)

By using the word boundary \b, regex engine backtracks (in-order to find a word boundary within 1 to 30 chars) and then it tries to match a word boundary and also the following one or more word chars.

DEMO

OR

^.{1,30}\K\b\w+

DEMO

Upvotes: 1

Related Questions