Adding Comments to a Regular Expression

By default, whitespace in a regular expression is significant. By specifying the COMMENTS flag, whitespace can be ignored in a pattern. While in comment mode, a particular whitespace character can be matched using a character class (for example, [\ ] matches a space).

In comment mode, the # character and everything following it is ignored. This feature is useful when a pattern is read from a file. Comment mode allows the pattern to span multiple lines and be commented.

It is also possible to enable comments within a pattern using the inline modifier (?x). The inline modifier affects all characters to the right and in the same enclosing group, if any. For example, in the pattern a(b(?x)c)d, only c is allowed to have comments. You can also force no comments with (?-x).

The inline modifier can also contain pattern characters using the form (?x:abc). In this case, only those pattern characters inside the inline modifier's enclosing group are affected. This form does not capture text (see Capturing Text in a Group in a Regular Expression).

CharSequence inputStr = "a b"; String patternStr = "a b"; // Compile without comments Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.matches(); // true // Compile with comments pattern = Pattern.compile(patternStr, Pattern.COMMENTS); matcher = pattern.matcher(inputStr); matchFound = matcher.matches(); // false // Use COMMENTS but include a character class with a space patternStr = "a [\\ ] b"; pattern = Pattern.compile(patternStr, Pattern.COMMENTS); matcher = pattern.matcher(inputStr); matchFound = matcher.matches(); // true // Use an inline modifier matchFound = pattern.matches("a b", inputStr); // true matchFound = pattern.matches("(?x)a b", inputStr); // false matchFound = pattern.matches("(?x)a [\\ ] b", inputStr); // true matchFound = pattern.matches("(?x)a \\s b", inputStr); // true matchFound = pattern.matches("a (?x: b )", inputStr); // true // Tabs and newlines in the pattern are ignored as well matchFound = pattern.matches("(?x)a \t\n \\s b", inputStr); // true // Read pattern from file try { File f = new File("pattern.txt"); FileReader rd = new FileReader(f); char[] buf = new char[(int)f.length()]; rd.read(buf); patternStr = new String(buf); matcher = pattern.matcher(inputStr); matchFound = matcher.matches(); // true } catch (IOException e) { }
Here are the contents of pattern.txt:
# This pattern matches ``a b'' a # this is the first part [\ ] # this is the second part b # this is the third part

Comments

3 Sep 2010 - 1:54am by Anonymous (not verified)

Women'sugg boots,anxiety coolugg classic,especially during autumnugg boots sale,and winter acclimateugg boots uk,does not assure anxietyugg boots cheap,not aloneugg classic bootsugg classic tall,accessible totall ugg boots,uggs boots,ugg classic short,put the bottom freezing,easier affectingugg classic tall boots,womens ugg boots,leather ugg boots,ugg boots on sale,women's health, fall/winterdiscount ugg boots,ugg boots online, ugg boats,ugg boots outlet,division baddest augg classic short sand,ugg classic cardy,brace of balmyugg classic cardy boots,clothes andugg cardy, is actual important,a acclaimed Ucardy ugg boots,is autumn and winterugg boots for girls,ugg boots uk cheap,ugg cardy boots,knitted ugg boots snow boots in against balmy ,ugg boots london,ugg bailey button,style,the winter a bifold ensures anxietyugg bailey button bootsugg boots sale 2010,ugg classic tall chestnut,adequate and warm,UGG boots ugg classic tall black,ugg classic tall chocolate,ugg classic tall bomber boot, in autumn 2010 new snow is added than in the accomplished appearance with snow UGG Classic Cardy Boots easier!

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.