[英]Specific regular expression pattern
假設我有這個示例字符串
<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>
我想使用正則表達式只有當完整的字符串以/one-two-three
開頭並以hh:mm
結尾時才能獲得2個結果。 我想得到:
/one-two-three/menus/13:00
/one-two-three/schedule/could be multiple delimiters/14:00
我試過正則表達式/one-two-three[\\s\\S]+?[0-9][0-9]:[0-9][0-9]
但這給了
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
我可以看到為什么我得到結果,但我的問題是我可以使用什么模式來排除沒有hh:mm
部分hh:mm
,其中可以有/one-two-three
和hh:mm
之間的任意數量的分隔符hh:mm
如果HTML結構對您很重要,那么正則表達式是錯誤的方法 。
否則(如果你可以匹配任何地方的字符串,只要它被"
)包圍,你可能想嘗試這個:
/one-two-three[^"]+?[0-9][0-9]:[0-9][0-9]
[\\s\\S]
基本上是指任何角色。 但是你只想要不是"
字符,因為這標志着路徑的終點。
嘗試
搜索".*\\"/{one-two-three}{.*}{[0-9][0-9]:[0-9][0-9]}.*"
用。。。來代替
\\1 = one-two-three
\\2 = middle parts
\\3 = hh:mm
如果你用\\1\\3
替換它將消除中間部分
希望這可以幫助 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.