Home > Backend Development > PHP Tutorial > 这样单双符号里面的值,正则应该如何写?

这样单双符号里面的值,正则应该如何写?

WBOY
Release: 2016-06-23 14:01:50
Original
908 people have browsed it

本帖最后由 u013670343 于 2014-02-22 21:44:46 编辑

proId:       '123456789',
'proId':'123456789',

第一个问题:想要拿到 123456789  兼容上面2行代码的正则该如何写?
就是无论是拿 proId:       '123456789',  里面的值,
还是拿  'proId':'123456789',  里面的值,
只写一个正则,都能拿值!



'newsId':'987654321',
newsId:"987654321",

第二个问题:想要拿到 987654321  兼容上面2行代码的正则该如何写?
就是无论是拿  'newsId':'987654321',  里面的值,
还是拿 newsId:"987654321",  里面的值,
只写一个正则,都能拿值!

回复讨论(解决方案)

[']*(?:proId|newsId)[']*:\s*[']*(\d+)[']*

[']*(?:proId|newsId)[']*:\s*[\'\"]*(\d+)[\'\"]*

第一个问题

$s=<<<txtproId:       '123456789','proId':'123456789',txt;preg_match_all("/proId'?\:\s*'(\d+)/",$s,$m);print_r($m[1]);
Copy after login


第二个问题
$s=<<<txt'newsId':'987654321',newsId:"987654321",txt;preg_match_all("/newsId'?\:('|\")(\d+)/",$s,$m);print_r($m[2]);
Copy after login

第一个:
/(\'){0,1}newsId(\'){0,1}:(\'|\")(\d+)(\'|\"),/

第二个
/(\'){0,1}userId(\'){0,1}:(\s*?){0,1}\'(\d+)\',/

$s =<<< TXTproId:       '123456789','proId':'123456789','newsId':'987654321',newsId:"987654321",TXT;preg_match_all('/:\s*([\'"])(.+)\1/', $s, $r);print_r($r[2]);
Copy after login
Array
(
    [0] => 123456789
    [1] => 123456789
    [2] => 987654321
    [3] => 987654321
)

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template