使用正则表达式将行拆分为多个部分
Split line into multiple parts using regex
我有一个像
这样的字符串
BK0001 My book (4th Edition) .95 (Clearance Price!)
我想要一种将它分成不同部分的方法,例如
[BK0001]
[My Book (4th Edition)]
[.95]
[(Clearance Price!)]
我是正则表达式的新手,我正在使用它来解析文件中的一行。我设法通过使用
获得了第一部分 BK0001
$parts = preg_split('/\s+/', 'BK0001 My book (4th Edition) .95 (Clearance Price!)';
然后获取 $part[0]
值,但不确定如何拆分它以获得其他值。
您可以使用带有捕获组的单一模式来匹配输入字符串的特定部分:
preg_match('~^(?<code>\S+)\s+(?<name>.*?)\s+(?<num>$\d[\d.]*)\s*(?<details>.*)$~', $text, $matches)
见regex demo。实际上,最后的 $
不是必需的,它只是为了显示整个字符串匹配。
详情
^
- 字符串的开头
(?<code>\S+)
- 组 "code":一个或多个非空白字符
\s+
- 1+ 个空格
(?<name>.*?)
- 组 "name":除换行字符外的任何 0+ 个字符,尽可能少
\s+
- 1+ 个空格
(?<num>$\d[\d.]*)
- 组 "num":一个 $
,然后是 1 个数字,然后是 0+ 个数字或 .
\s*
- 0+ 个空格
(?<details>.*)
- 组 "details":除换行字符外的任何 0+ 个字符,尽可能多
$
- 字符串结尾。
$re = '~^(?<code>\S+)\s+(?<name>.*?)\s+(?<num>$\d[\d.]*)\s*(?<details>.*)$~';
$str = 'BK0001 My book (4th Edition) .95 (Clearance Price!)';
if (preg_match($re, $str, $m)) {
echo "Code: " . $m["code"] . "\nName: " . $m["name"] . "\nPrice: " .
$m["num"] . "\nDetails: " . $m["details"];
}
输出:
Code: BK0001
Name: My book (4th Edition)
Price: .95
Details: (Clearance Price!)
尝试使用 preg_match
$book_text = "BK0001 My book (4th Edition) .95 (Clearance Price!)";
if(preg_match("/([\w\d]+)\s+(.*?)\s+\((.*?)\)\s+(\$[\d\.]+)\s+\((.*?)\)$/",$book_text,$matches)) {
//Write code here
print_r($matches);
}
$matches[0] 保留用于完整匹配字符串。您可以从 $matches[1]...
中找到拆分部分
Array ( [0] => BK0001 My book (4th Edition) .95 (Clearance Price!) [1] => BK0001 [2] => My book [3] => 4th Edition [4] => .95 [5] => Clearance Price! )
$matches[1] is "book number"
$matches[2] is "book name"
$matches[3] is "edition"
$matches[4] is "price"
$matches[5] is "special text"
我有一个像
这样的字符串BK0001 My book (4th Edition) .95 (Clearance Price!)
我想要一种将它分成不同部分的方法,例如
[BK0001]
[My Book (4th Edition)]
[.95]
[(Clearance Price!)]
我是正则表达式的新手,我正在使用它来解析文件中的一行。我设法通过使用
获得了第一部分BK0001
$parts = preg_split('/\s+/', 'BK0001 My book (4th Edition) .95 (Clearance Price!)';
然后获取 $part[0]
值,但不确定如何拆分它以获得其他值。
您可以使用带有捕获组的单一模式来匹配输入字符串的特定部分:
preg_match('~^(?<code>\S+)\s+(?<name>.*?)\s+(?<num>$\d[\d.]*)\s*(?<details>.*)$~', $text, $matches)
见regex demo。实际上,最后的 $
不是必需的,它只是为了显示整个字符串匹配。
详情
^
- 字符串的开头(?<code>\S+)
- 组 "code":一个或多个非空白字符\s+
- 1+ 个空格(?<name>.*?)
- 组 "name":除换行字符外的任何 0+ 个字符,尽可能少\s+
- 1+ 个空格(?<num>$\d[\d.]*)
- 组 "num":一个$
,然后是 1 个数字,然后是 0+ 个数字或.
\s*
- 0+ 个空格(?<details>.*)
- 组 "details":除换行字符外的任何 0+ 个字符,尽可能多$
- 字符串结尾。
$re = '~^(?<code>\S+)\s+(?<name>.*?)\s+(?<num>$\d[\d.]*)\s*(?<details>.*)$~';
$str = 'BK0001 My book (4th Edition) .95 (Clearance Price!)';
if (preg_match($re, $str, $m)) {
echo "Code: " . $m["code"] . "\nName: " . $m["name"] . "\nPrice: " .
$m["num"] . "\nDetails: " . $m["details"];
}
输出:
Code: BK0001
Name: My book (4th Edition)
Price: .95
Details: (Clearance Price!)
尝试使用 preg_match
$book_text = "BK0001 My book (4th Edition) .95 (Clearance Price!)";
if(preg_match("/([\w\d]+)\s+(.*?)\s+\((.*?)\)\s+(\$[\d\.]+)\s+\((.*?)\)$/",$book_text,$matches)) {
//Write code here
print_r($matches);
}
$matches[0] 保留用于完整匹配字符串。您可以从 $matches[1]...
中找到拆分部分Array ( [0] => BK0001 My book (4th Edition) .95 (Clearance Price!) [1] => BK0001 [2] => My book [3] => 4th Edition [4] => .95 [5] => Clearance Price! )
$matches[1] is "book number"
$matches[2] is "book name"
$matches[3] is "edition"
$matches[4] is "price"
$matches[5] is "special text"