import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\bCUI\\b";
final String string = "/**\n"
+ "Contributors: Charles Staich\n"
+ "Match Confidence: Low\n"
+ "Language: Regular Expressions (regex)\n"
+ "Language Version: \n"
+ "Flags:\n"
+ "Purpose: Match CUI Basic and CUI Specified. Note the importance of evaluating multiple possible labels, such that CUI Specified may also be considered if this pattern matches. CUI Specified requires *at least* the same protections as CUI Basic (citation needed).\n"
+ "Assumptions: CUI Basic and CUI Specified documents contain the phrase 'CUI'\n"
+ "Plain English Pattern Narrative: \n"
+ " Case sensitive, and does not match 'cui'.\n"
+ " Indiscriminately matches any single instance of the word* 'CUI' without any additional qualifiers, context, or markings necessary (Low Confidence)\n"
+ "Capturing Group(s): None\n"
+ "Definitions:\n"
+ " Word: A string of alphanumeric characters surrounded by word boundaries (\\b).\n"
+ "Comments:\n"
+ " Do not use this in Production without heavy testing.\n"
+ " I realize now why the community has taken to using the word cooey. I'm certain there have been many collective hours spent reviewing false positive matches they created through filing standards, policy, communications, and documentation.\n"
+ "**/\n\n"
+ "// PATTERN:\n"
+ "\\bCUI\\b\n\n"
+ "// POSITIVE CASES:\n"
+ "CUI\n"
+ "asdf CUI jkl;\n"
+ "This document does not contain CUI.\n"
+ "This portal is not permitted to be used for transferring CUI.\n"
+ "No CUI allowed.\n"
+ "===CUI===\n"
+ "(CUI)\n"
+ "***CUI***\n"
+ "^CUI^\n"
+ "!CUI!\n"
+ "~CUI~\n"
+ "CUI//SP-TEST\n"
+ "CUI//TEST\n\n\n"
+ "// NEGATIVE CASES:\n"
+ "cui\n"
+ "0CUI-\n"
+ "ACUI*\n"
+ "-CUI2\n"
+ "CUi CuI cUI\n"
+ "AAACUIAAA\n"
+ "Cooey\n"
+ "Circuit\n"
+ "CIRCUIT\n"
+ "CUISP\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS | Pattern.DOTALL | Pattern.UNICODE_CASE | Pattern.UNICODE_CHARACTER_CLASS);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html