[英]php RegEx extract values from string
I am new to regular expressions and I am trying to extract some specific values from this string: 我是正则表达式的新手,并且尝试从此字符串中提取一些特定的值:
"Iban: EU4320000713864374\\r\\nSwift: DTEADCCC\\r\\nreg.no 2361 \\r\\naccount no. 1234531735"
“ Iban:EU4320000713864374 \\ r \\ n快捷方式:DTEADCCC \\ r \\ nreg.no 2361 \\ r \\ n帐户编号1234531735”
Values that I am trying to extract: 我尝试提取的值:
EU4320000713864374
2361
This is what I am trying to do now: 这就是我现在想要做的:
preg_match('/[^Iban: ](?<iban>.*)[^\\r\\nreg.no ](?<regnr>.*)[^\\r\\n]/',$str,$matches);
All I am getting back is null or empty array. 我所得到的只是空数组或空数组。 Any suggestions would be highly appreciated
任何建议将不胜感激
The square brackets make no sense, you perhaps meant to anchor at the beginning of a line: 方括号没有意义,您可能打算将其锚定在行的开头:
$result = preg_match(
'/^Iban: (?<iban>.*)\R.*\R^reg.no (?<regnr>.*)/m'
, $str, $matches
);
This requires to set the multi-line modifier (see m
at the very end). 这需要设置多行修饰符(请参阅最后的
m
)。 I also replaced \\r\\n
with \\R
so that this handles all kind of line-separator sequences easily. 我还更换
\\r\\n
与\\R
,使得该易处理所有种类线分离器序列。
Example: https://eval.in/47062 示例: https : //eval.in/47062
A slightly better variant then only captures non-whitespace values: 稍微好一点的变体则仅捕获非空白值:
$result = preg_match(
'/^Iban: (?<iban>\S*)\R.*\R^reg.no (?<regnr>\S*)/m'
, $str, $matches
);
Example: https://eval.in/47069 示例: https : //eval.in/47069
Result then is (beautified): 结果为(美化):
Array
(
[0] => "Iban: EU4320000713864374
Swift: DTEADCCC
reg.no 2361"
[iban] => "EU4320000713864374"
[1] => "EU4320000713864374"
[regnr] => "2361"
[2] => "2361"
)
preg_match("/Iban: (\\S+).*reg.no (\\S+)/s", $str, $matches);
关于换行符有一个特殊的功能:点(。)与换行符不匹配,除非指定了s
标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.