duanfoumi5620 2013-09-03 07:31
浏览 109
已采纳

反直觉的preg_match行为 - 匹配一系列字符的最简洁方法是什么?

I am looking for ways to match a range of characters and assumed the following regEx would only match characters in the range of hex codes between 20 and 7E. However, it also matches chr(10) (line feed), in fact, the following prints "passed":

echo preg_match("/^[\x20-\x7E]*$/", chr(10)) ? 'passed' : 'failed';

Any idea why and how to match that range?

  • 写回答

1条回答 默认 最新

  • douque8861 2013-09-03 07:43
    关注

    chr(10) is end of line, so you should add modifier D.

    If this modifier is set, a dollar metacharacter in the pattern matches only at the end of the subject string. Without this modifier, a dollar also matches immediately before the final character if it is a newline (but not before any other newlines).

    //                               v
    echo preg_match("/^[\x20-\x7E]*$/D", chr(10)) ? 'passed' : 'failed';
    //                               ^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏