【问题标题】:Regular Expression is failing in java正则表达式在java中失败
【发布时间】:2017-01-04 02:58:59
【问题描述】:

我有一个以 %payslip%.xml.gpg 开头的文件名。

以下是可能的文件名示例:

Taswkly_payslips_Pay27.xml.gpg
exec_payslip.xml.gpg
Cairns_payslips_adv_P27.xml.gpg

您能帮我建议上述模式名称的正则表达式吗?

在上面的模式中,下面的事情是固定的,即

*payslip*.xml.gpg.

任何帮助将不胜感激。

【问题讨论】:

  • 试试.*payslip.*\\.xml\\.gpg

标签: java regex regex-lookarounds


【解决方案1】:

你可以使用这个正则表达式:

^.*payslip.*\.xml\.gpg$

^            start of the line
.*           any character multiple times
payslip      the string "payslip"
.*           any character multiple times
\.           the "." character
xml          the string "xml"
\.           the "." character
gpg          the string "gpg"
$            end of the line

也别忘了在java中转义

^.*payslip.*\\.xml\\.gpg$

Working example

【讨论】:

  • 当然,如果您使用String 类的matches 方法,则不需要^$
猜你喜欢
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多