0

I have strings which look like this:

/xxxxx/xxxxx-xxxx-xxxx-338200.html

With my regex:

(?<=-)(\d+)(?=\.html)

It matches just the numbers before .html.

Is it possible to write a regex that matches everything that surrounds the numbers (matches the .html part and the part before the numbers)?

1
  • 2
    What tool/language are you using here? Commented Nov 21, 2019 at 16:28

2 Answers 2

2

In your current pattern you already use a capturing group. In that case you might also match what comes before and after instead of using the lookarounds

-(\d+)\.html

To get what comes before and after the digits, you could use 2 capturing groups:

^(.*-)\d+(\.html)$

Regex demo

In the replacement use the 2 groups.

Sign up to request clarification or add additional context in comments.

Comments

2

This should do the job:

.*-\d+\.html

Explanation: .* will match anything until -\d+ say it should match a - followed by a sequence of digits before a \.html (where \. represents the character .).

To capture groups, just do (.*-)(\d+)(\.html). This will put everything before the number in a group, the number in another group and everything after the number in another group.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.