[英]How do I extract last element between angle brackets using regular expressions?
[英]Extract whatever is in brackets using regular expressions
我真的根本不了解正則表達式,這很傷我的頭。
我有些文字看起來像這樣
blah blah blah (here is the bit I'd like to extract)
...而且我不太了解如何使用PHP的preg_split或等效命令來提取此內容。
我該怎么做呢? 在哪里可以了解預浸料的工作原理?
這樣的事情應該可以解決問題,以匹配(
和)
之間的內容:
$str = "blah blah blah (here is the bit I'd like to extract)";
if (preg_match('/\(([^\)]+)\)/', $str, $matches)) {
var_dump($matches[1]);
}
你會得到:
string 'here is the bit I'd like to extract' (length=35)
基本上,我使用的模式搜索:
(
;但是as(具有特殊含義,必須轉義: \\(
[^\\)]+
([^\\)]+)
$matches[1]
)
; 在這里,這也是一個必須轉義的特殊字符: \\)
<?php
$text = "blah blah blah (here is the bit I'd like to extract)";
$matches = array();
if(preg_match('!\(([^)]+)!', $text, $matches))
{
echo "Text in brackets is: " . $matches[1] . "\n";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.