java - Include Trailing Character for Regex -
i have regex made java search words:
virtue virtues virtue virtues
the line of code here:
pattern p = pattern.compile("\\bvirtue[s']?\\b", pattern.case_insensitive);
however, need include trailing character. example:
virtue's or virtue's
can tell me else needed trailing character?
you can try following regular expression:
(?i)\bvirtue('?s)?\b
additionally, if plan use regular expression often, recommended use constant in order avoid recompile each time, e.g.:
private static final pattern regex_pattern = pattern.compile("(?i)\\bvirtue('?s)?\\b"); public static void main(string[] args) { string input = "virtue\nvirtues\nvirtue\nvirtues\nvirtue's or virtue's"; matcher matcher = regex_pattern.matcher(input); while (matcher.find()) { system.out.println(matcher.group()); } }
output:
virtue virtues virtue virtues virtue's virtue's
Comments
Post a Comment