dongsou0083 2017-01-23 14:32
浏览 81

正则表达式多行模式无法按预期方式为可选组[重复]工作

This question already has an answer here:

I have a next text source:

PHPUnit 5.7.5 by Sebastian Bergmann and contributors.

.....................E.....R.................................   61 / 1485 (  1%)
.....................                                         1485 / 1485 (100%)

Time: 1.51 minutes, Memory: 102.00MB

---error details skipped---

ERRORS!
Tests: 1485, Assertions: 14821, Errors: 1, Failures: 1.

I need to parse in real time (streamed output to stdout) next symbols: EFWIRS. (error, failure, warning, etc) and if available statistics line (numbers and percent in EOL). So, in results I except something like that:

.......

or

.....................E.....R.................................

or

.....................E.....R.................................   61 / 1485 (  1%)

or

.....................                                         1485 / 1485 (100%)

but not

.....................E.....R.................................   61 / 1485 (

or

.....................E.....R.................................   61 / 1485 (  1

or

.....................E.....R.................................   61 /

My regex is /^[EFWIRS.]+\s*(?:\d+\s\/\s\d+\s\(\s*\d+%\) )?/m but it also match extra ERR string in case if tests failed (look at output example). I try /^[EFWIRS.]+\s*(?:\d+\s\/\s\d+\s\(\s*\d+%\))?$/m, but this regex is skip lines with incompleted statistics block completely and match only whole string despite the optional group (...)?, that works in first regex as expected.

P.S. I know about custom printers for PHPUnit, but in my case they are not suitable and I need to parse common output.

</div>
  • 写回答

2条回答 默认 最新

  • dscs63759 2017-01-24 05:46
    关注

    I believe you have to use lookahead to ensure both correct characters to match and correct consequence of them in a line.

    I've made an example for you here. The pattern itself is ^([EFWIRS.](?=[EFWIRS.\s]))+\s+(\d+\s*\/\s*\d+\s*\(\s*\d+%\s*\))?$ I am not sure about your mechanism of output capturing so can't advise on modifiers and which PHP regex functions to use. Example down by the link assumes full multiline text so it uses global and multiline modifiers (meaning preg_match_all and #pattern#m would be used in php code).

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分