regular expressions starting and ending with a fixed string in java

huangapple go评论65阅读模式
英文:

regular expressions starting and ending with a fixed string in java

问题

\*122\*12345678#
如何匹配这个字符串,使得\*122\*是开头,#是结尾,中间有8个数字?

英文:

\*122\*12345678#

How to match this string for *122* is start, # is end, and 8 numbers in the middle?

答案1

得分: 3

^\*122\*(\d{8})#$

  • ^\*122\**122* 开头(* 需要使用 \ 转义)
  • (\d{8}) 8 位数字
  • #$ 以 # 结尾

你可以使用在线网站 https://regexper.com/ 来测试和检查你的正则表达式。

正如 @Zabuzard 指出的,如果你不需要捕获数字并将整个字符串作为搜索字符串,你可以使用更简单的表达式:

\*122\*\d{8}#

英文:
^\*122\*(\d{8})#$
  • ^\*122\* start with *122* (* need to be escaped using \)
  • (\d{8}) 8 digits
  • #$ ends with #

You can test and check your regex expression using online sites as https://regexper.com/

As @Zabuzard noted, if you don't need to capture digits and search string as a whole, you can use simpler expression:

\*122\*\d{8}#

huangapple
  • 本文由 发表于 2020年8月12日 19:09:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63375264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定