user857521
user857521

Reputation:

Specific regular expression pattern

Say I have this example string

    <td><a href="/one-two-three/menus" title="test"</td>
<td><a href="/one-two-three/menus/13:00 title="test"</td>
<td><a href="/one-two-three/schedule/could be multiple delimiters/14:00 title="test"</td>

I want to use regex to get 2 results only when the full string starts with /one-two-three and ends with hh:mm. Eg I want to get:

/one-two-three/menus/13:00
/one-two-three/schedule/could be multiple delimiters/14:00

I've tried regex pattern /one-two-three[\s\S]+?[0-9][0-9]:[0-9][0-9]

but this gives

Found 2 matches:
1./one-two-three/menus" title="test"</td>     <td><a href="/one-two-three/menus/13:00
2./one-two-three/schedule/could be multiple delimiters/14:00

I can see why I get the results but my question is what pattern can I use to exclude parts without hh:mm where there can be any number of delimiters between /one-two-three and hh:mm

Upvotes: 0

Views: 99

Answers (2)

Icarus
Icarus

Reputation: 455

try

search ".*\"/{one-two-three}{.*}{[0-9][0-9]:[0-9][0-9]}.*"

replace with

\1 = one-two-three \2 = middle parts \3 = hh:mm

if you replace with \1\3 it will eliminate middle portion

Hope this helps :)

Upvotes: 0

Martin Ender
Martin Ender

Reputation: 44289

If the HTML structure is important to you, regex is the wrong approach.

Otherwise (if you can match the string anywhere as long as it's surrounded by "), you might want to try this:

/one-two-three[^"]+?[0-9][0-9]:[0-9][0-9]

[\s\S] basically mean any character. But you only want characters that are not ", because this marks the end of the path.

Upvotes: 2

Related Questions