移动规则适配:正则表达式撰写范例

1、纯数字替换生成pattern例子:

eg1:url对应关系:

http://www.abc.com/26299483.html-> http://m.abc.com/26299483.html
pattern:
http://www.abc.com/([0-9]+).html-> http://m.abc.com/${1}.html

eg2:url对应关系:

http://www.abc.com/t26299483.html-> http://m.abc.com/26299483.html
pattern:
http://www.abc.com/t([0-9]+).html-> http://m.abc.com/${1}.html

2、纯字母替换生成pattern例子:

eg:url对应关系:

http://www.abc.com/fawliute/ -> http://m.abc.com/fawliute/
pattern:
http://www.abc.com/([a-zA-Z]+)/ -> http://m.abc.com/${1}/

3、字母和数字混合的字符串替换生成pattern的例子:

eg1:url对应关系:

http://www.abc.com/a1cc1n2q5y3/ -> http://m.abc.com/a1cc1n2q5y3/
pattern:
http://www.abc.com/((?:[a-zA-Z]+[0-9]+|[0-9]+[a-zA-Z]+)[a-zA-Z0-9]+)/ -> http://m.abc.com/${1}/

注意:字母和数字混合字符串,字母和数字必须交替出现至少1次

有效例子:a13b,23a9,da3bc99,42a1c

eg2:url对应关系:

http://news.abc.com/09/1001/07/5KH8DE1F000120GR.html
-> http://m.abc.com/news/09/1001/07/5KH8DE1F000120GR.html
pattern:
http://news.abc.com/([0-9]+)/([0-9]+)/([0-9]+)/([ a-zA-Z0-9]+).html
-> http://m.abc.com/news/${1}/${2}/${3}/${4}.html

4、对于字母和数字只交替出现一次的,可以分别用数字和字母进行正则替换:

eg:url对应关系:

http://www.abc.com/az123/ -> http://m.abc.com/az123/
pattern:
http://www.abc.com/([a-zA-Z]+)([0-9]+)/-> http://m.abc.com/${1}${2}/

5、中文字符串正则替换生成pattern例子:

eg:url对应关系:

http://www.abc.com/长城花园/ -> http://m.abc.com/长城花园/
pattern:
http://www.abc.com/((?:%[a-zA-Z0-9]{2,})+)/-> http://m.abc.com/${1}/

6、由’-‘或者’_’连接的数字或者字母替换生成pattern的例子:

eg:url对应关系:

http://www.abc.com/byd-c3/-> http://m.abc.com/byd-c3/
pattern:
http://www.abc.com/([a-zA-Z]+)-([a-zA-Z]+)([0-9]+)/->http://m.abc.com/${1}-${2}${3}/
注意:’-‘和’_’出现多次可以使用同样的方式处理
如:abc-134_x-1

7、对参数部分进行正则替换生成pattern的例子:

eg:url对应关系:

http://www.abc.com/article.html?act=test&id=123 -> http://m.abc.com/article.html?act=test&id=123
pattern:
http://www.abc.com/article\.html?act=([^&]+)&id=([^&]+) -> http://m.abc.com/article.html?act=${1}&id=${2}

8、PC存在分页对应移动页面生成pattern的例子:

eg:url对应关系:

http://www.a.com/1234-1.htm http://www.a.com/1234-2.htm ->http://m.a.com/1234.htm
pattern:
http://www.a.com/([0-9]+)-([0-9]+).htm -> http://m.a.com/${1}.htm

未经允许不得转载:陈海飞博客 » 移动规则适配:正则表达式撰写范例

分享到:更多 ()

评论 20

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #20

    1 hour payday loans no credit check loans loans bad credit loans

    Loan Cash1周前 (07-10)回复
  2. #19

    loans for people with bad credit loans online loans bad credit a loan with bad credit

    A Payday Loan1周前 (07-10)回复
  3. #18

    When I originally commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get four emails with the same comment. Is there any approach you possibly can remove me from that service? Thanks!

  4. #17

    loans for bad credit best loans for bad credit online loans bad credit a loan with bad credit

    Pay Day Loans1周前 (07-11)回复
  5. #16
  6. #15
  7. #14

    online payday loan direct lenders best loans for bad credit loan for bad credit cash america payday advance

    Loan7天前回复
  8. #13

    direct online payday lenders loans guaranteed approval payday loans bad credit

  9. #12

    My spouse and i were absolutely peaceful Edward managed to conclude his survey through the entire precious recommendations he acquired through the site. It’s not at all simplistic just to continually be making a gift of strategies that many many people have been making money from. We really grasp we’ve got the website owner to thank for that. The illustrations you’ve made, the simple website menu, the friendships your site aid to foster – it’s got everything wonderful, and it’s making our son in addition to our family know that the content is awesome, and that is truly vital. Thank you for the whole lot!

  10. #11
  11. #10
  12. #9

    loans loans no teletrack payday loans loans for bad credit

  13. #8

    loans for people with bad credit a loan with bad credit direct consolidation loan online loans bad credit

  14. #7

    speedy loan with bad credit easy installment loans online bad credit loan

  15. #6

    best loans for bad credit unsecured personal loans bad credit loan with bad credit loan for bad credit

  16. #5
  17. #4

    payday loans for bad credit bad credit installment loan payday loans bad credit paydayloan

  18. #3

    payday loans for bad credit direct consolidation loan loans bad credit loans

  19. #2

    My wife and i have been very glad Louis managed to carry out his studies from the ideas he got out of your web pages. It’s not at all simplistic just to choose to be giving away key points which often most people might have been making money from. We really keep in mind we have got the website owner to thank for that. The most important explanations you have made, the straightforward site menu, the relationships you can assist to engender – it’s got mostly awesome, and it is aiding our son in addition to us do think this topic is fun, and that is highly indispensable. Thanks for everything!

  20. #1

    loans no fax installment loans online loans bad credit loans