Browsium

Knowledge Base

Using regular expressions in Ion rules

Applies to

Ion 2.x or Ion 3.0

Summary

The Ion Configuration Manager tries to make it simple to create rules to match your web applications. In general, the basic rule condition (e.g. “Begins With” or “Includes”) selections will suffice for matching your application and applying the correct profile. In some cases, however, the simple rule conditions may not meet your requirements and you will need a more powerful tool. The rule condition, “RegEx”, can be used to create rules that match more complicated URI patterns. This article will explain how regular expressions may be used in Ion to create more powerful rules.

Overview of Regular Expressions

Regular expressions provide a language for specifying a pattern of text. Whereas simple string matching rules require that the text to match be a precise sequence of characters, regular expressions allow you to match patterns that include repetitions, a variable number of elements or wildcards.

Ion uses the Microsoft .Net Framework implementation of regular expressions. However, a full description of how to use regular expressions is beyond the scope of this article. For a more complete discussion of regular expressions, we recommend you follow the “Further Reading” section below. The rest of this article will offer some commonly used elements, provide an example rule and explain how to test your regular expressions.

This table provides a small sampling of commonly used regular expression elements.

Element Description Pattern Matches
\ When followed by a character that is not recognized as an escaped character, matches that character. This allows the regular expression engine to disambiguate language elements (such as * or ?) and character literals (represented by \* or \?) \d+[\+-x\*]\d+\d+[\+-x\*\d+ “2+2” and “3*9” in “(2+2) * 3*9”
\w Matches any word character \w “I”, “D”, “A”, “1”, “3” in”ID A1.3″
\W Matches any non-word character \W ” “. “.” in”ID A1.3″
\d Matches any decimal digit \d “4” in”4 = IV”
\D Matches any non-decimal digit \D ” “, “=”, ” “, “I”, “V” in”4 = IV”
^ The match must start at the beginning of the string or line ^\d{3} “901-” in”901-333″
$ The match must occur at the end of the string or before \n at the end of the line or string -\d{3}$ “-333″ in”901-333”
( subexpression ) Captures the matched subexpression and assigns it a zero-based ordinal number (\w)\1 “ee” in “deep”
* Matches the previous element zero or more times \d*\.\d “.0”, “19.9”, “219.9”
+ Matches the previous element one or more times “be+” “bee” in “been”, “be” in “bent”
? Matches the previous element zero or one time “rai?n” “ran”, “rain”
{ n } Matches the previous element exactly n times “,\d{3}” “,043” in “1,043.6”, “,876”, “,543”, and “,210” in “9,876,543,210”
{ n ,} Matches the previous element at least n times “\d{2,}” “166”, “29”, “1930”
{ n , m } Matches the previous element at least n times, but no more than m times “\d{3,5}” “166”, “17668”

Rule example

The basic rule condition selections available in Ion only allow you to match on a single element of the URI. This is usually sufficient, but you may have situations where no single element of the URI can uniquely identify your web application. To avoid creating a rule that matches too aggressively, we can construct a rule with a “RegEx” condition that matches more precisely on the elements of the URI that uniquely identify your web application.

For this example, we will use the scenario of an organization that has two web applications running on the same web servers. One of those web applications requires Internet Explorer 9 while the other requires the Adaptive IE Quirks engine.

  Application A Application B
Hostname www.browsermechanics.com www.browsermechanics.com
Port 80 8080
IE Version 9 Adaptive IE Quirks

We want to create a rule that will match Application B and apply the “Adaptive IE Quirks” engine while leaving requests to Application A alone so that they render as usual.

To make things a little more complicated, let’s also say that there are multiple hostnames that can be used to access your applications, but that the domain name (browsermechanics.com) is always the same.

Valid URIs to match on in this scenario would include:

http://www.browsermechanics.com:8080/foo/bar.html

http://app.browsermechanics.com:8080/something/else.html

URIs that we would not want to match on would include:

http://www.browsermechanics.com/index.html

http://www.browsermechanics.com:80/foo/bar.html

 

Creating Your Rule:

  1. Start in the Ion Configuration Manager in the Rules Manager (make sure you’ve already created an ‘Adaptive IE Quirks’ engine profile
  2. Give your rule a name (“Browsermechanics.com app”)
  3. In the “Element” select box, choose “Absolute Uri”
  4. In the “Operator” box, choose “RegEx”
  5. In the “Value” box, enter “\w+\.browsermechanics\.com:8080”
  6. We want Ion to render the page using the “Adaptive IE Quirks” engine, so select that profile from the open select box. You should have something that looks like this:
    ionregex
  7. Finally, click on the OK button to create your new rule

 

Explanation:

  • “\w+” matches one or more word characters, e.g. “www” or “app”
  • “\.” matches the literal character “.” which is reserved in regular expressions
  • “browsermechanics”, “com” and “:8080” match the literal strings

Testing your regular expressions

Regular expressions can be complex, so it is very important to test them before deploying to ensure proper pattern matching. To streamline the testing process, the Ion Configuration Manager provides a tool for quickly evaluating regular expressions. The RegEx Builder tool can be accessed in the Ion Configuration Manager by navigating to the “RegEx Builder” node under “Tools” in the Objects pane, on the left side of the manager.

 

Using the RegEx Builder:

  1. Enter the expression you want to test into the Regular Expression box
  2. Enter multiple strings that you want to test against in the Text box
  3. Finally, hit the Test Expression button

 

Here’s what you’ll get if you use the regular expression and URIs from our example above:

ionregex2

Note that our regular expression only matched URIs that were intended for port 8080 at browsermechanics.com, but did not match URIs on other ports. This is precisely the behavior we were hoping to achieve.

Further Reading

Microsoft provides a detailed analysis of how regular expressions are used within the .Net Framework. For those interested in more detailed information, we recommend these articles:

Regular Expression Language Elements

http://msdn.microsoft.com/en-us/library/az24scfc.aspx

 

Regular Expression Examples

http://msdn.microsoft.com/en-us/library/kweb790z.aspx

 

The definitive guide to using regular expressions is provided by O’Reilly Media

Mastering Regular Expression

http://oreilly.com/catalog/9781565922570

Posted in: Ion Knowledge Base

  • Share:  

Request Demo

FLASH END OF LIFE - We have a solution to allow you to use flash securely after it's been deprecated.SCHEDULE A CALL