faceoff
faceoff

Reputation: 901

Match the whole string that contains a known string

I have a string like this ./this-needs-to-be-matched/knownstring/endofstring.

I always know that the endofstring is 11 chars long

I have used this until I realized that the - sign will not be matched

\w+\/knownstring\/\w{11}

Now I don't really know how to match any char, including the minus sign.

The result should be: The result should be: this-needs-to-be-matched/knownstring/endofstring

I have also tried:

\.\/.*\/knownstring\/\w{11}

And different other variations so I'm stuck.

Upvotes: 0

Views: 30

Answers (2)

Bohemian
Bohemian

Reputation: 425448

Add - to the list of allowed chars:

^\.\/[\w-]+\/knownstring\/\w{11}$

or just "not a slash":

^\.\/[^\/]+\/knownstring\/\w{11}$

Upvotes: 1

ivanibash
ivanibash

Reputation: 1491

I think \.\/.*\/knownstring\/\w{11} should actually work, what exactly is wrong with this?

If endofstring needs to match exactly 11 characters and no more, you can add a word boundary symbol \b.

\.\/.*\/knownstring\/\w{11}\b

You can test this here.

Upvotes: 0

Related Questions