Php 字符串的特殊预匹配

Php 字符串的特殊预匹配,php,regex,string,preg-match,Php,Regex,String,Preg Match,这是我的字符串: ================================================================================ INPUT FILE ================================================================================ NAME = CO-c0m1.txt | 1> !

这是我的字符串:

================================================================================
                                       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个小时,但做不到,请帮助,这是我的比赛:

$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]);
      }
我对regex很陌生,似乎很难适应。 有人能帮我吗,先谢谢:)


$res
的第二项是一个包含所需内容的数组。

您需要一个正则表达式,该表达式匹配以
|
开头的行,后跟一些空格,然后是一个或多个数字,然后是
,并且只需要该前缀后面的文本

正则表达式是:
/^\\s*\d+>(.*)$/m
。它包含一个用于捕获所需文本的组。将捕获片段放入
$matches[1]
中:

preg_match_all('/^\|\s*\d+>(.*)$/m', $source[$i], $matches);
echo(implode("\n", $matches[1]));
然后,您可以通过其他方式(,等)删除包含
****输入结束****
的行

检查它的运行情况:

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

阅读更多信息。

您不需要在文本上运行第一个正则表达式,只需运行此正则表达式:

preg_match_all("/[|]\s*\d*[>]\s(.+)/", $source[$i], $matches);
echo(implode("\n", $matches[1]));

这在我的测试中效果很好。

您可以使用单个正则表达式解决方案一次性获取所有这些数据:

^\|\h+\d+>(?!\h*\Q****END OF INPUT****\E)\h\K.+
细分:

  • ^
    匹配行首
  • \\124;\ h+\d+>
    匹配到
    数字>
  • (?!
    开始负面展望
    • \h*
      如果存在水平空白
    • \Q****输入结束****\E
      并以输入结束结束
  • 前瞻结束
  • \h\K
    匹配水平空白,然后重置匹配
  • +
    匹配到行尾
PHP代码:

preg_match_all("~^\|\h+\d+>(?!\h*\Q****END OF INPUT****\E)\h\K.+~mi", $str, $matches);

print_r($matches[0])的输出


您需要执行
内爆(PHP_EOL,$matches[0])将值连接在一起。

您试图实现什么?获取除空白之外的所有行?就像输出一样,删除| n>和换行符您可以弹出最后一个元素以获得OP所需的准确输出。我猜主要问题是正则表达式,我已修复了它,是的,他/她可以轻松删除最后一项。是的,对于OP来说,更重要的是不要尝试使用一个正则表达式获取所有内容,因为这会使它变得更加复杂。非常感谢您的回答,Idk如果我的代码因为我的原始preg_match原始txt而无法工作:这是我的新代码:无打印:(用新答案回答你:原始txt:Idk如果我的代码因为我的原始preg__匹配而不起作用,这是我的新代码:使用我们的anwser但数组是0谢谢你这么多这实际上起了作用,我在最后一行摆脱了你的欢迎,但你签署了另一个人的答案作为最佳答案!这是一个愚蠢的错误,他回答了fir但是我很感激你,抽出时间来安慰我。
preg_match_all("~^\|\h+\d+>(?!\h*\Q****END OF INPUT****\E)\h\K.+~mi", $str, $matches);
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] => *
)