[英]regex that covers multiple lines
I would like to extract the data from the following sample txt 我想从以下示例txt中提取数据
START
A: 567 B:345
C: 345 D:485
START
A: 345 B:267
C: 980 D:054
I'm using this regex with no positive results: 我使用此正则表达式没有积极的结果:
$regex = '~'
. '^(?P<header>.*START:.*)$'
//. '^(.*A: \d{3} B: \d{3}.*)$'
//. '(.*C: \d{3} D: \d{3}.*)$'
. '~m'
;
That gives me the following result: 这给了我以下结果:
Array
(
[0] => Array
(
[0] => START:
[header] => START:
[1] => START:
)
[1] => Array
(
[0] => START:
[header] => START:
[1] => START:
)
)
But as soon as I uncomment the lines 但是一旦我取消评论
. '^(.*A: \\d{3} B: \\d{3}.*)$'
and 和
. '(.*C: \\d{3} D: \\d{3}.*)$'
I get no match! 我没有比赛!
How can I extract this data? 如何提取这些数据?
I've tried deleting the start of line anchor ^
and changing the end of line anchor $
to \\n
with no success 我尝试删除行锚
^
的开头并将行锚$
的结尾更改为\\n
,但没有成功
How can I extract the data when I have a start of data line followed by n data lines, followed by a end of data line followed by one or more empty lines? 当我有一条数据线的开头,n条数据线,一条数据线的结尾, 一条或多条空线时,如何提取数据?
Try this: 尝试这个:
(START)\s+(A:\s+\d+\s+B:\d+)\s+(C:\s+\d+\s+D:\d+)
MATCHES: 火柴:
MATCH 1
1. [0-5] `START`
2. [6-18] `A: 567 B:345`
3. [19-31] `C: 345 D:485`
MATCH 2
1. [34-39] `START`
2. [40-52] `A: 345 B:267`
3. [53-65] `C: 980 D:054`
While matching multiline string with modifier m (MULTILINE
, you have to make sure you are matching following newline with [\\r\\n]
or better with \\R
. 在将多行字符串与修饰符
m (MULTILINE
,必须确保在换行符之后与[\\r\\n]
匹配,或者与\\R
匹配。
For your input following regex should work: 输入以下正则表达式即可:
/^(?<header>START)$\R+^(?<line1>A:\h*\d{3}\h+B:\h*\d{3})$\R+^(?<line2>C:\h*\d{3}\h+D:\h*\d{3})$/m
\\R
matches any newline and \\h
matches any horizontal whitespace in PCRE. \\R
匹配任何换行符, \\h
匹配PCRE中的任何水平空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.