当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - 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

网友答案:

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.

网友答案:

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 :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: