Amer Hamid
Amer Hamid

Reputation: 149

Regex to extract if match occurs within first N characters

I am new to regex still in learning phase. I wrote an regex expression to extract until first occurrence of a colon:

^([^:]+):

Now I want to take it one step further and limit the search to within first 100 characters. Which mean no match if match does not occur within first 100 characters and I don't know how to amend this expression to do the needful.

Upvotes: 1

Views: 1304

Answers (2)

stack0114106
stack0114106

Reputation: 8711

Check this

(?!^[^:]{100})(^[^:]+):$

https://regex101.com/r/dBOzVT/2

Upvotes: 0

jiwopene
jiwopene

Reputation: 3627

Try this:

^([^:]{1,100}):

This regex matches all text from the start of line/text to the first colon only if there is 1–100 characters before the colon ({1,100} instead of +).

Upvotes: 3

Related Questions