我有一个网址:
https://my.site.com/u/0/ac?export=download&confirm=45vy&id=qNhdhk1jejhXLexLpY3RiDY2oamis">D
我想用preg_match_all来匹配它。我的regex表达式是:
preg_match_all('/(https:\/\/my\.site\.com\/[u]\/[0]\/(ac)\/(?)\/.*\">D)/', $input_lines, $output_array);
但在上面的代码中,我无法匹配特殊字符?
。我试过使用(?)
。但它不是匹配的。我知道这可能是个蹩脚的问题,但如果有人能帮助我匹配?
或在preg_match_all中逃离?
,那将是有帮助的。
发布于 2021-01-02 14:43:25
我刚刚注意到,在ac之后,链接中没有/
,但是在regex中添加了它,所以只要尝试删除它或使用下面的代码,它的工作和测试就可以了。
<?php
$input_lines = 'https://my.site.com/u/0/ac?export=download&confirm=45vy&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中使用/
作为可选参数。
<?php
$input_lines = 'https://my.site.com/u/0/ac?export=download&confirm=45vy&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的两个链接。
发布于 2021-01-02 15:53:44
你的审判官
/(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
- ...mark the start and end of a pattern; similar to single/double quotes marking the start and end of strings
- 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:
- To avoid escaping you can use a different delimiter
模式2:~https://www.website.com/page/1/index.php~
2.因为你只想匹配字面上的字符,所以你可以简单地使用模式中的字符。只有当字符可以是多个值时,才需要字符集。
Set Matched value
u ===> u
[u] ===> u
[ua] ===> u OR a
2
一样,这里不需要捕获组,因为您只对捕获整个字符串感兴趣。这将将$output_array[1] = "ac"
添加到输出中。
/
,因此模式将永远不会返回任何?
是regex中的一个特殊字符;通常用于组(a
)的开头,修改量词(b
),或暗示构造是可选的(c
)。在本例中,(?)
绝对什么也不做;.*
与文本?
匹配,或者如果斜杠不在模式中,则会执行。a.在一个组中使用?
可以表示,例如:
(?:.) ===>非捕获组(?=.) ===>正前瞻(?!) ===>负前瞻
修改量词:通常情况下,量词+
或*
是贪婪的,并且尽可能匹配。将一个?
放在它之后,使它不再贪婪,并在第一种可能的情况下停止。
字符串: IIIIO模式匹配/I*O/IIIIO/I*?
c.使构造为可选的
模式匹配1匹配2解释~https?:// http:// https://可选字符~(?:www.)?website.com~ website.com www.website.com可选非捕获组
5b
--这是一个贪婪的量词,因此,例如,如果模式\">D
在字符串中出现不止一次,这将匹配到最后一次出现。- 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
~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
码
$input_lines = 'https://my.site.com/u/0/ac?export=download&confirm=45vy&id=qNhdhk1jejhXLexLpY3RiDY2oamis">D';
preg_match_all('~https://my\.site\.com/u/0/ac\?.*?">D~', $input_lines, $output_array);
print_r($output_array);
输出
Array
(
[0] => Array
(
[0] => https://my.site.com/u/0/ac?export=download&confirm=45vy&id=qNhdhk1jejhXLexLpY3RiDY2oamis">D
)
)
https://stackoverflow.com/questions/65540216
复制相似问题