首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >特殊字符的preg_match_all [?]

特殊字符的preg_match_all [?]
EN

Stack Overflow用户
提问于 2021-01-02 14:30:58
回答 2查看 148关注 0票数 2

我有一个网址:

代码语言:javascript
运行
复制
https://my.site.com/u/0/ac?export=download&confirm=45vy&id=qNhdhk1jejhXLexLpY3RiDY2oamis">D

我想用preg_match_all来匹配它。我的regex表达式是:

代码语言:javascript
运行
复制
preg_match_all('/(https:\/\/my\.site\.com\/[u]\/[0]\/(ac)\/(?)\/.*\">D)/', $input_lines, $output_array);

但在上面的代码中,我无法匹配特殊字符?。我试过使用(?)。但它不是匹配的。我知道这可能是个蹩脚的问题,但如果有人能帮助我匹配?或在preg_match_all中逃离?,那将是有帮助的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-02 14:43:25

我刚刚注意到,在ac之后,链接中没有/,但是在regex中添加了它,所以只要尝试删除它或使用下面的代码,它的工作和测试就可以了。

代码语言:javascript
运行
复制
<?php

$input_lines = 'https://my.site.com/u/0/ac?export=download&amp;confirm=45vy&amp;id=qNhdhk1jejhXLexLpY3RiDY2oamis">D';
preg_match_all('/(https:\/\/my\.site\.com\/[u]\/[0]\/(ac)(\?).*\">D)/', $input_lines, $output_array);

var_dump($output_array);

这是输出- https://prnt.sc/weq86u

或者,如果有可能在ac/?之后发生,则可以尝试在regex中使用/作为可选参数。

代码语言:javascript
运行
复制
<?php

$input_lines = 'https://my.site.com/u/0/ac?export=download&amp;confirm=45vy&amp;id=qNhdhk1jejhXLexLpY3RiDY2oamis">D';
preg_match_all('/(https:\/\/my\.site\.com\/[u]\/[0]\/(ac)\/?(\?).*\">D)/', $input_lines, $output_array);

var_dump($output_array);

它将匹配带有或不带/ https://prnt.sc/weqbae的两个链接。

票数 3
EN

Stack Overflow用户

发布于 2021-01-02 15:53:44

你的审判官

代码语言:javascript
运行
复制
/(https:\/\/my\.site\.com\/[u]\/[0]\/(ac)\/(?)\/.*\">D)/
^                           ^    ^    ^   ^ ^    ^     ^
1                           2    2    3   4 5    6     1
+-- Starting delimiter      |    |    |   | |    |     +-- Ending delimiter
                            |    |    |   | |    +-- This is a greedy match and may not stop where intended
                            |    |    |   | +-- `?` is a special character in Regex and does nothing in this scenario; the .* is actually matching the `?`
                            |    |    |   +-- This slash doesn't exist
                            |    |    +-- No need for a capture group
                            +----+-- No need for a character set

  1. 正则表达式模式分隔符:

代码语言:javascript
运行
复制
- ...mark the start and end of a pattern; similar to single/double quotes marking the start and end of strings
代码语言:javascript
运行
复制
- As with quotes if you use the delimiter in the pattern you have to escape it

/https:\/\/www.website.com\/page\/1\/\index.php/模式1:

代码语言:javascript
运行
复制
- To avoid escaping you can use a different delimiter

模式2:~https://www.website.com/page/1/index.php~

2.因为你只想匹配字面上的字符,所以你可以简单地使用模式中的字符。只有当字符可以是多个值时,才需要字符集。

代码语言:javascript
运行
复制
   Set       Matched value
   u    ===> u
   [u]  ===> u
   [ua] ===> u OR a

  1. 2一样,这里不需要捕获组,因为您只对捕获整个字符串感兴趣。这将将$output_array[1] = "ac"添加到输出

中。

  1. ,由于某种原因,您试图匹配URL中不存在的/,因此模式将永远不会返回任何

  1. ?是regex中的一个特殊字符;通常用于组(a)的开头,修改量词(b),或暗示构造是可选的(c)。在本例中,(?)绝对什么也不做;.*与文本?匹配,或者如果斜杠不在模式中,则会执行。

a.在一个组中使用?可以表示,例如:

(?:.) ===>非捕获组(?=.) ===>正前瞻(?!) ===>负前瞻

修改量词:通常情况下,量词+*是贪婪的,并且尽可能匹配。将一个?放在它之后,使它不再贪婪,并在第一种可能的情况下停止。

字符串: IIIIO模式匹配/I*O/IIIIO/I*?

c.使构造为可选的

模式匹配1匹配2解释~https?:// http:// https://可选字符~(?:www.)?website.com~ website.com www.website.com可选非捕获组

  1. As per 5b --这是一个贪婪的量词,因此,例如,如果模式\">D在字符串中出现不止一次,这将匹配到最后一次出现。

代码语言:javascript
运行
复制
- i.e. if there were more than one URL in your string then it would match from the first until the last as opposed to matching them individually

字符串:链接1链接2模式匹配~website.com\?id=.*">~ 1 website.com?id=2432546t4534">Link 1~website.com\?id=.*?“> [1] website.com?id=2432546t4534"> [2] website.com?id=24345yr6787">

修复

更新的Regex

代码语言:javascript
运行
复制
~https://my\.site\.com/u/0/ac\?.*?">D~
~                                      : Starting delimiter
 https://my\.site\.com/u/0/ac          : Matches the initial part of the URL
                             \?        : Matches a literal ?
                               .*?     : Non-greedy match any character 0 or more times
                                  ">D  : Match string literally
                                     ~ : Ending delimiter

代码语言:javascript
运行
复制
$input_lines  = 'https://my.site.com/u/0/ac?export=download&amp;confirm=45vy&amp;id=qNhdhk1jejhXLexLpY3RiDY2oamis">D';

preg_match_all('~https://my\.site\.com/u/0/ac\?.*?">D~', $input_lines, $output_array);

print_r($output_array);

输出

代码语言:javascript
运行
复制
Array
(
    [0] => Array
        (
            [0] => https://my.site.com/u/0/ac?export=download&confirm=45vy&id=qNhdhk1jejhXLexLpY3RiDY2oamis">D
        )

)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65540216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档