Jackson YAML: mapping a regex Pattern with flags(Jackson YAML:使用标志映射正则表达式模式)
问题描述
在 Jackson 中,我可以映射 YAML 中的字符串:
In Jackson, I can map a string in YAML:
regexField: "(\d{2}):(\d{2})"
到类上的 Pattern
字段:
final class MappedFromYaml {
private Pattern regexField;
// ... accessors
}
Jackson 的 ObjectMapper
将创建一个带有默认标志的 Pattern
.是否可以使用设置的特定标志来创建它,例如 Pattern.MULTILINE
?理想情况下,我希望能够在 YAML 中指定这些标志,但如果未能在 Java 代码中为特定字段指定标志的解决方案也将不胜感激.
Jackson's ObjectMapper
will create a Pattern
with default flags. Is it possible to make it create it with specific flags set, such as Pattern.MULTILINE
? Ideally I would like to be able to specify those flags in YAML, but failing that a solution that specifies the flags for a specific field in Java code would also be appreciated.
推荐答案
有两种方法.第一个是将标志直接嵌入 到正则表达式中:
There are two ways. The first is embedding flags directly into the regex:
regexField: "(\d{2}):(\d{2})(?m)"
否则不要直接映射到Pattern
,而是引入一个自定义类型,比如PatternBuilder
Otherwise don't map directly to Pattern
, but introduce a custom type like a PatternBuilder
public class PatternBuilder {
public String regex;
public boolean multiline;
public Pattern pattern() {
int flags = 0;
if (multiline) flags |= Pattern.MULTILINE;
return Pattern.compile(regex, flags);
}
}
可以从 YAML 构建
that can be built from the YAML
pattern:
regex: "(\d{2}):(\d{2})"
multiline: true
这篇关于Jackson YAML:使用标志映射正则表达式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jackson YAML:使用标志映射正则表达式模式


基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01