sgdjs
sgdjs

Reputation: 21

How to exclude parts of a line in regex

Out of the line Your name is: "Foo Bar" I want to select Foo Bar in regex only.

I have tried non-capturing groups without success : (?:^Your name is: ").*(?:")$

"(.*?)" Works but I don't want the double quotes to be selected

Upvotes: 0

Views: 60

Answers (2)

The fourth bird
The fourth bird

Reputation: 163207

As you tagged pcre, you don't have to use a lookbehind assertion but you can match the text and then use \K to forget what is matched so far.

^Your name is: "\K.+(?="$)

See a regex demo.

Or without lookarounds at all and a capture group:

^Your name is: "(.+)"$

See another regex demo.

Upvotes: 2

VvdL
VvdL

Reputation: 3210

You can use lookbehind and lookahead:

(?<=^Your name is: ").+(?="$)

(?<= looks behind for Your name is: " and (?= looks ahead for ".

The result will be whatever is between that.

regex101

Upvotes: 1

Related Questions