![](/img/trans.png)
[英]I want to extract a part of the string from a string using regular expression
[英]Extract information from string using regular expression
我想使用正則表達式提取“軟件”及其值“2”的信息信息。 請在PHP中查看下面的字符串。
$str = "http://abcfastdirectory.com vs http://www.weblinkstoday.com/detail/link-116406.htm ::-(1)**abcfastdirectory(1)**allows(1)**create(1)**directories(1)**professional(1)**((1)**abcfastdirectory(1)**allows(1)**club(1)**contact(1)**create(2)**details(1)**directories(4)**directory(3)**for(3)**it(1)**our(1)**page(1)**professional(2)**software(2)**"
如何使用PHP中的正則表達式從上面的字符串中提取信息?
如果您只是在“軟件(2)”之后,則應執行以下操作:
preg_match('/software\((?<value>\d+)\)/', $str, $m);
print $m['value'];
但是,如果要匹配<word>(<num>)
每個部分,可以使用以下內容:
preg_match_all('/(?<key>\w+)\((?<value>\d+)\)/i', $str, $m);
foreach ($m['key'] as $i => $key) {
print $key.' => '.$m['value'][$i]."\n";
}
$software = (int)preg_replace('/.*software\((\d+)\).*/','$1',$str);
嘗試這個:
$pattern = '/\*\*software\([0-9]+\)\*\*/';
preg_match($pattern, $str, $matches);
// your value will be stored in $matches[1]
您可以嘗試這個並查看結果,如果它是您正在尋找的:
<?php
$str = "http://abcfastdirectory.com vs http://www.weblinkstoday.com/detail/link-116406.htm ::-(1)**abcfastdirectory(1)**allows(1)**create(1)**directories(1)**professional(1)**((1)**abcfastdirectory(1)**allows(1)**club(1)**contact(1)**create(2)**details(1)**directories(4)**directory(3)**for(3)**it(1)**our(1)**page(1)**professional(2)**software(2)**";
$matches = array();
preg_match_all('#\*\*(.*?)\((\d+)\)\*\*#',$str,$matches, PREG_SET_ORDER );
print_r($matches);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.