【问题标题】:Spring Expression Language validate String by contains() or RegexSpring Expression Language 通过 contains() 或 Regex 验证字符串
【发布时间】:2017-08-28 09:46:27
【问题描述】:

我需要通过正则表达式验证字符串值 我所拥有的是一种在 XML 文档中使用 Spring 表达式语言的方法

component.previewImage.mime == 'image/png'

这就是我目前所拥有的。我希望验证 mime 类型字符串是否包含 image。 我不太喜欢春天的表情。

伪:

component.previewImage.mime.contains('image')

这可能吗?或者在这里使用正则表达式的方法?

编辑: 实际部分工作表达式:

newCMSComponent.detailText.values().^[!#empty(#this)] != null AND (newCMSComponent.videoUrl != null OR (newCMSComponent.video != null AND newCMSComponent.video.mime == 'video/mp4')) AND newCMSComponent.previewImage != null AND newCMSComponent.previewImage.mime matches 'image\/'"

请注意,newCMSComponent.previewImage.mime matches 'image\/' 还不起作用 - 这是我尝试实现的。

【问题讨论】:

  • 您是否尝试执行XSD validation
  • 否 - 仅在 XML 文档中使用上述表达式。这样做的目的是通过内容验证字段。

标签: xml string spring-el


【解决方案1】:

由于contains()String 上的一个方法,您可以按原样使用它。

但是,SpEL 确实支持使用 matches 运算符的 RegEx - 请参阅 here

"'5.00' matches '\^-?\\d+(\\.\\d{2})?$'"

【讨论】:

  • 遗憾地使用contains() 不起作用。将尝试表达式 sn-p
  • "newCMSComponent.previewImage.mime matches 'image\/'" 这就是我使用的表达方式。如果出现任何类型的image/pngimage/jpeg 等,它应该匹配。我有什么问题吗?
  • matches 需要完全匹配,而不是部分匹配;试试'image\/.*'
猜你喜欢
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多