[英]Special preg_match for a string
这是我的字符串:
================================================================================
INPUT FILE
================================================================================
NAME = CO-c0m1.txt
| 1> ! HF def2-TZVP opt numfreq
| 2>
| 3> % scf
| 4> convergence tight
| 5> end
| 6>
| 7> * xyz 0 1
| 8> C 0 0 0
| 9> O 0 0 1
| 10> *
| 11>
| 12> ****END OF INPUT****
================================================================================
我想得到这个输出:
! HF def2-TZVP opt numfreq
% scf
convergence tight
end
* xyz 0 1
C 0 0 0
O 0 0 1
*
我一直试图做5个小时而不能做,请帮助,这是我的pregmatch:
$regx = '/INPUT FILE...................................................................................(.*?)........................END OF INPUT/s';
if(preg_match($regx, $source[$i], $matches)) {
$input[$i] = preg_replace('/\s\s\s\s+/', "\n", $matches[1]);
}
我是正则表达式的新手,似乎很难。 有人可以帮助我,提前谢谢:)!
$p ="/[|]\s*\d*[>]\s(.+)/";
$t = "================================================================================
INPUT FILE
================================================================================
NAME = CO-c0m1.txt
| 1> ! HF def2-TZVP opt numfreq
| 2>
| 3> % scf
| 4> convergence tight
| 5> end
| 6>
| 7> * xyz 0 1
| 8> C 0 0 0
| 9> O 0 0 1
| 10> *
| 11>
| 12> ****END OF INPUT****
================================================================================";
preg_match_all($p,$t,$res);
die(json_encode($res[1], JSON_PRETTY_PRINT));
/* Output:
[
"! HF def2-TZVP opt numfreq",
"% scf",
" convergence tight",
"end",
"* xyz 0 1",
"C 0 0 0",
"O 0 0 1",
"*",
" ****END OF INPUT****"
]
*/
$res
第二项是一个拥有你想要的数组。
您需要一个与以|
开头的行匹配的正则表达式 然后是一些空格,然后是一个或多个数字然后>
,你只需要这个前缀后面的文字。
正则表达式为:/ /^\\|\\s*\\d+>(.*)$/m
.*) /^\\|\\s*\\d+>(.*)$/m
。 它包含您需要的文本的捕获组。 preg_match_all()
将捕获片段放在$matches[1]
:
preg_match_all('/^\|\s*\d+>(.*)$/m', $source[$i], $matches);
echo(implode("\n", $matches[1]));
然后,您可以通过其他方式( array_pop()
, array_filter()
等删除包含****END OF INPUT****
的行)
检查它的实际操作: https : //3v4l.org/hUEBk
regex
解释说:
/ # regex delimiter
^ # match the beginning of the line
\| # match '|' (it needs to be escaped because it is a meta-character)
\s # match a whitespace character (space, tab)
* # the previous (a whitespace) can appear zero or more times
\d # match a digit (0..9)
+ # the previous (a digit) can appear one or more times
> # match '>'
( # begin of a capturing group
.* # match any character, any number of times
) # end of the capturing group
$ # match the end of the line
/ # regex delimiter
m # multiline (regex modifier); check the regex against each line of the input string
阅读更多关于PHP中Perl兼容正则表达式的信息 。
您不需要在文本上运行第一个正则表达式,只运行此正则表达式:
preg_match_all("/[|]\s*\d*[>]\s(.+)/", $source[$i], $matches);
echo(implode("\n", $matches[1]));
这在我的测试中工作正常。
您可以使用单个正则表达式解决方案一次性获取所有这些数据:
^\|\h+\d+>(?!\h*\Q****END OF INPUT****\E)\h\K.+
分解:
^
匹配行的开头 \\|\\h+\\d+>
匹配digit>
(?!
开始否定前瞻
\\h*
如果存在horizental空格(s) \\Q****END OF INPUT****\\E
以输入结束结束 )
前瞻的结束 \\h\\K
匹配一个horizental空格然后重置匹配 .+
匹配到行尾 PHP代码:
preg_match_all("~^\|\h+\d+>(?!\h*\Q****END OF INPUT****\E)\h\K.+~mi", $str, $matches);
print_r($matches[0]);
输出print_r($matches[0]);
:
Array
(
[0] => ! HF def2-TZVP opt numfreq
[1] => % scf
[2] => convergence tight
[3] => end
[4] => * xyz 0 1
[5] => C 0 0 0
[6] => O 0 0 1
[7] => *
)
你需要做一个内implode(PHP_EOL, $matches[0]);
将价值观融合在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.