AboDa7aM
AboDa7aM

Reputation: 21

RegEx for matching a string between some characters?

I would like to get the text from (character/s) to another (character/s), excluding the searching characters (meaning whatever in between the two searching terms but not including them)

For example this text

href="https://old.reddit.com/user/TKayOKAY"> class="author"

using the regex /user/(.*?)"/g returned "user/TKayOKAY""

How to remove user/ and " the end double quotes, to get only the username?

basically returning >> TKayOKAY for example.

Upvotes: 0

Views: 27

Answers (2)

Denis Bogdanov
Denis Bogdanov

Reputation: 1

I would decide it so

/user/([A-z]+)

I have excluded "common" expressions like ". *", In order to avoid subtle problems. If you see that the string contains only letters, then look for only letters.

Upvotes: 0

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

If your regex dialect supports lookarounds, you can use lookarounds based regex so your full match is the only intend text.

(?<=/user/)(.*?)(?=")

Regex Demo

OR you can capture your data from group1 using your own regex mentioned in your post.

/user/(.*?)"

Regex Demo

This demo will show your intended text capture in present in group1

Upvotes: 1

Related Questions