In Jackson, I can map a string in YAML:
regexField: "(\\d{2}):(\\d{2})"
to a Pattern
field on a class:
final class MappedFromYaml {
private Pattern regexField;
// ... accessors
}
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)"
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);
}
}
that can be built from the YAML
pattern:
regex: "(\\d{2}):(\\d{2})"
multiline: true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.