如何从正则表达式匹配中排除一些URL [英] how to exclude some url from a regex match

查看:368
本文介绍了如何从正则表达式匹配中排除一些URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种类型的付款(在线,离线,余额)每种付款类型都有不同的网址,所以我使用正则表达式来匹配所有此网址:

1 - 订单/结帐/未付/已完成

<订单/结帐/付款/完成> Authority = 000000000000000000000000000039067905& Status = OK
3-订单/结帐/付款/完成?状态= OK



正则表达式:(。*?)完成

这个正则表达式匹配所有但问题是它匹配一些页面! kile本产品页: b
$ b

/ tork- 完成 r-motahari-tehran

什么是解决方案?

解决方案

您可以使用

 <$ c $ (。*)C>完成($ | [?])

的一点是,你需要匹配字符串末尾的 done 或最后。该模式会匹配




  • (。*) - 任何0+字符,如尽可能多,直到后来的子模式的最后一次出现为止

  • done - 文字子字符串 done

  • ($ | []) - 要么 $ (字符串结尾)或者是一个文字(可以写成 \? ,要点是前面没有方括号或 \ 意味着 1或0重复 左侧的原子)。


    请注意,您可以通过添加转组到非捕获如果你不使用捕捉组值。


    I have 3 type payment ( online, offline, by balance) every payment type have different url so I'm using regex for match all this url :

    1- order/checkout/unpaid/done

    2- order/checkout/paid/done?Authority=000000000000000000000000000039067905&Status=OK 3- order/checkout/paid/done?Status=OK

    regex : (.*?)done

    this regex match all of urls but the problem is it match some page too ! kile this product page :

    /tork-doner-motahari-tehran

    What is the solution ?

    解决方案

    You may use

    (.*)done($|[?])
    

    The point is that you need to match done at the end of the string or at the last ?. The pattern will match

    • (.*) - any 0+ characters, as many as possible, up to the last occurrence of the subsequent subpatterns
    • done - a literal substring done
    • ($|[?]) - either $ (end of string anchor) or a literal ? (can be written as \? in the pattern, the main point being that ? without brackets or a \ in front means 1 or 0 repetitions of the atom to the left of the ?).

    Note that you may turn the groups into non-capturing by adding ?: after ( if you are not using the capturing group values.

    这篇关于如何从正则表达式匹配中排除一些URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆