簡體   English   中英

字符串的特殊preg_match

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM