duanpa1980 2018-11-15 15:54
浏览 60
已采纳

preg_match()失败了

I need to check if a string contains a table, which is like this:

+--------+------+-------+
| <info>number</info> | <info>char</info> | <info>word</info>  |
+--------+------+-------+
| 1      | a    | alfa  |
| 2      | b    | beta  |
| 3      | c    | gamma |
+--------+------+-------+

I do not know the number of columns, nor the number of rows, but this is the structure of the table.

This regex works with Unix, but not with Windows

[\+\-]+[
](\|(\s+<info>[^<]+<\/info>\s+\|)+)[
](\+|\-)+[
]((\|(\s+[^\|]+\s+\|)+[
])+)(\+|\-)+

This is a test:
https://regex101.com/r/TSxSd7/1

And this is a part of code:

    $regexRowDivider = '[\+\-]+';
    $regexHeader = '(\|(\s+<info>[^<]+<\/info>\s+\|)+)';
    $regexRow = '\|(\s+[^\|]+\s+\|)+';
    $regexRows = '((' . $regexRow . '[
])+)';
    $regexTable = $regexRowDivider . '[
]' . $regexHeader . '[
]' . $regexRowDivider . '[
]' . $regexRows . $regexRowDivider;

    preg_match('/' . $regexTable . '/', $output, $matches);

After hours of testing, I can not figure out what the problem is. Do you have any idea? preg_last_error() returns a 0 (no errors)

  • 写回答

1条回答 默认 最新

  • duanli4146 2018-11-16 06:17
    关注

    This is probably because you're only accepting one of either or , and Windows uses both ( ). You can match both of these newline characters (and more) by using \R.

    I removed a few pieces of complexity, so it doesn't enforce spaces in every cell:

    [+-]+\R+\|(\s*<info>[^<]+<\/info>\s*\|)+\R+[+-]+\R+\|([^|]+\|)+\R+[+-]+
    
    \---/   \------------------------------/   \---/   \----------/   \---/
    Line             Column Headers            Line      Contents     Line
    

    Try it online

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?