I want to match the nth hyphen.

Asked 1 years ago, Updated 1 years ago, 81 views

I want to match the nth hyphen in the regular expression.
For example, I would like to know the regular expression that matches and removes the right -fdfd-fd from the second hyphen from the left in the URL below.

If you want to match it from the first time, it's -.*.

https://www.example.com/a-12345-fdfd-fd

regular-expression

2022-09-30 21:44

1 Answers

This is an example of a regular expression to match for the second time

^[^-]*-[^-]*-

You can delete it from the second time to the right with perl

s/^([^-]*-[^-]*)-.+$/\1/


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.