nowucca.com - personal software technology blog

Thanks to this post, I've discovered how to use case insensitive regular expressions.

Simply start your regular expression with "?i:".  So "(?i:REGEX)" matches REGEX case insensitively.

In the code example below, one can use this to extract out an attribute value inside quotes when the attribute name is case-insensitive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<br />
public static void main(String[] args) {<br />
        String example = &quot;somePrefix NAME=\&quot;value\&quot; someSuffix&quot;;</p>
<p>        Pattern p = Pattern.compile(&quot;(.*)\\s(?i:name=)(\&quot;(.*)\&quot;)\\s(.*)&quot;);<br />
        Matcher m = p.matcher(example);<br />
        if (m.matches()) {</p>
<p>            MatchResult mr = m.toMatchResult();</p>
<p>            for( int i = 0; i &amp;lt;= mr.groupCount(); i++) {<br />
                System.out.println(&quot;Group &quot;+i+&quot;: &quot;+mr.group(i));<br />
            }<br />
        }</p>
<p>        if ( m.matches() &amp;amp;&amp;amp; m.groupCount()&amp;gt;=3) {<br />
            System.out.println(&quot;Extracted value: &quot;+m.group(3));<br />
        }</p>
<p>    }<br />

The code output yielded is:

Group 0: somePrefix NAME="value" someSuffix
Group 1: somePrefix
Group 2: "value"
Group 3: value
Group 4: someSuffix
Extracted value: value