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 

regex

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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -