Php Regex:使用dot-all标志,如何给每行加前缀?
每行的前缀应该是什么 假设我有输入数据:Php Regex:使用dot-all标志,如何给每行加前缀?,php,regex,Php,Regex,每行的前缀应该是什么 假设我有输入数据: SOME OTHER DATA TABLE ROW ROW ROW END SOME OTHER DATA 我只感兴趣的是什么之间,包括表和结束 在php中,您可以编写如下/TABLE.*?END/s这样的正则表达式,它将表的第一次出现与END的第一次出现相匹配。但是有没有一种方法可以在每一行前面加上%?因此,结果将是: SOME OTHER DATA %TABLE %ROW %ROW %ROW %END SOME OTHER DATA 非常
SOME OTHER DATA
TABLE
ROW
ROW
ROW
END
SOME OTHER DATA
我只感兴趣的是什么之间,包括表和结束
在php中,您可以编写如下/TABLE.*?END/s这样的正则表达式,它将表的第一次出现与END的第一次出现相匹配。但是有没有一种方法可以在每一行前面加上%?因此,结果将是:
SOME OTHER DATA
%TABLE
%ROW
%ROW
%ROW
%END
SOME OTHER DATA
非常感谢您的帮助。您应该使用2个正则表达式:
$txt = file_get_contents('input.txt');
preg_match("#(.*(?<=TABLE\n))(.*\nEND)(.*)#ms",$txt,$m);
$new = $m[1].preg_replace("#^#ms","%",$m[2]).$m[3];
print $new;
$txt=file\u get\u contents('input.txt');
preg#u match(“#)(*(您应该使用2个正则表达式:
$txt = file_get_contents('input.txt');
preg_match("#(.*(?<=TABLE\n))(.*\nEND)(.*)#ms",$txt,$m);
$new = $m[1].preg_replace("#^#ms","%",$m[2]).$m[3];
print $new;
$txt=file\u get\u contents('input.txt');
preg_match(“#)(.*(您可以使用单个preg_replace()
)进行匹配,这两种模式假定TABLE和END后面紧跟一个换行符(或END的字符串结尾),前面紧跟一个换行符(或TABLE的字符串开头):
此代码的行为与第一个模式相同,请注意,您可以使用UNIX格式的换行符获取字符串。您可以使用一个preg_replace()
,这两个模式假设TABLE和END后面紧跟着一个换行符(或END的字符串结尾),前面紧跟着一个换行符(或表格字符串的开头):
此代码的行为与第一个模式相同,请注意,您使用UNIX格式的换行符获取字符串。在这里。我确实创建了一个正则表达式,并为您正确地注释了它:
/(?:
#start by finding the initial position of the table start, in order to store the match position for \G
TABLE\n\K|
#after we've found the table head, continue matching using this position. make sure we arent at the beginning of the string
\G(?<!^)
)
#capture the data we're interested in
(?:
#make sure there is no 'END' in the string
(?!END)
#match everything until the line ending
.
)*
#consume the newline at the end of the string
\n/x
/(?)
#首先查找表开始的初始位置,以便存储\G的匹配位置
表\n\K|
#找到表头后,使用此位置继续匹配。确保我们不在字符串的开头
\G(?给你。我确实创建了一个正则表达式,并为你恰当地注释了它:
/(?:
#start by finding the initial position of the table start, in order to store the match position for \G
TABLE\n\K|
#after we've found the table head, continue matching using this position. make sure we arent at the beginning of the string
\G(?<!^)
)
#capture the data we're interested in
(?:
#make sure there is no 'END' in the string
(?!END)
#match everything until the line ending
.
)*
#consume the newline at the end of the string
\n/x
/(?)
#首先查找表开始的初始位置,以便存储\G的匹配位置
表\n\K|
#找到表头后,使用此位置继续匹配。确保我们不在字符串的开头
\G(?最简单和最直接的方法是用两个表达式来实现。您介意提供一个示例吗?输入数据是在一行还是在一个数组中?最简单和最直接的方法是用两个表达式来实现。您介意提供一个示例吗?输入数据是在一行还是在一个数组中?
(?= # open the lookahead
(?s) # the s modifier allows dot to match newlines
.*? # zero or more characters (lazy)
# (note: the lazy quantifier with the lookbehind make the check
# very slow)
(?<= # open a lookbehind
\nEND\r # since a variable length lookbehind is not allowed,
| # you can however enumerate the different possibilities with
\nEND\n # the different types of newlines (\r\n->Windows, \n->UNIX)
|
\nEND$
) # close the lookbehind
) # close the lookahead
$arr = preg_split('/\R/', $txt);
$state = FALSE;
foreach ($arr as &$line) {
if ($state || $line == 'TABLE') {
$state = ($line == 'END')? FALSE : TRUE;
$line = '%' . $line;
}
}
$txt = implode("\n", $arr);
/(?:
#start by finding the initial position of the table start, in order to store the match position for \G
TABLE\n\K|
#after we've found the table head, continue matching using this position. make sure we arent at the beginning of the string
\G(?<!^)
)
#capture the data we're interested in
(?:
#make sure there is no 'END' in the string
(?!END)
#match everything until the line ending
.
)*
#consume the newline at the end of the string
\n/x