doubianyan9749 2014-10-22 14:41
浏览 57

正则表达式中混合字符之间的破折号的含义?

I'm just getting my feet wet with regexes and I came across this within a PHP program that someone else had written: [ -\w]. Note that the dash is not the first character, there is a space preceding it.

I can't make heads or tails of what it means. I know that the dash between characters inside brackets normally indicates a range, i.e. [a-z] matches any lowercase character "a" through "z", but what does it match when the dash is between characters of different types?

My first thought was that it just matches any space or alphanumeric character, but then the dash wouldn't be necessary. My second thought was that it's matching spaces, alphanumerics, and the dash; but then I realized that the dash would probably be either escaped or moved to the front or back for that.

I've googled around and can't find anything about using a dash in a character class with mixed characters. Maybe I'm using the wrong search terms.

  • 写回答

3条回答 默认 最新

  • dongmo2324 2014-10-22 14:49
    关注

    This might help : http://www.regular-expressions.info/charclass.html in the section "Metacharacters Inside Character Classes" it says :

    Hyphens at other positions in character classes where they can't form a range may be interpreted as literals or as errors. Regex flavors are quite inconsistent about this.

    My guess would be that it is being intepreted as a literal, so the regexp would match a space, hyphen or \w .

    As a reference, it looks invalid in PCRE: Debuggex Demo

    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数