douyabu1528 2013-10-21 05:05
浏览 414
已采纳

用于匹配两个或多个数字的正则表达式,然后是一个“x”,然后是两个或更多数字

I'm having trouble constructing this regex. I need a strpos or a preg_match that will match the following pattern: 00x00. where the '0's are any number(s), and x is upper or lowercase, then a period.

I know I can do \d for numbers, but don't know how to specify two or more numbers. I know I can do [xX] for either upper or lowercase 'x', and I'm lost on the period.

I've tried this:

preg_match ( '/\\d[xX]\\d\./', $string )

but that isn't working.

I'd prefer to do it with a strpos, but either is fine.

EDIT - The period is optional; it's just added security. But I'm not sure if it's possible to match it not knowing how many numbers are between the 'x' and the period.

  • 写回答

1条回答 默认 最新

  • douweng9427 2013-10-21 05:08
    关注

    Something like so: \d{2,}[Xx]\d{2,}\. will match two or more digits, an x (ignoring case) and two more digits and a period.

    If that is all you want to match, I would recommend adding the anchor tags: ^\d{2,}[Xx]\d{2,}\.$. This will instruct the regex engine to match exactly that.

    So in short, this \d{2,}[Xx]\d{2,}\. will match foo bar 000X00 foo bar but this: `^\d{2,}[Xx]\d{2,}\.$ will match only 000X00 and nothing else (so it will fail for my previous example).

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看