Using the Rx ToolkitKomodo IDE only
Creating Regular ExpressionsUse the Rx Toolkit to create and edit regular expressions. Create regular expressions by typing them in the Regular Expression pane. A regular expression can include metacharacters, anchors, quantifiers, digits, and alphanumeric characters. Many of the display characteristics available in the Editor Pane can be enabled in the Regular Expression field. However, these characteristics must be manually enabled via key bindings. For example, to display line numbers in the Regular Expression field, press 'Ctrl'+'Shift'+'6' ('Meta'+'Shift'+'6' on Mac OS X) if the default key binding scheme is in effect. Note: Do not enclose regular expressions in forward slashes ("/"). The Rx Toolkit does not recognize enclosing slashes. ![]() Adding Metacharacters to a Regular ExpressionThe Shortcuts menu provides a list of all of the metacharacters that are valid in the Rx Toolkit. To add a metacharacter to a regular expression:
If you already know the metacharacter you need, just type it in the Regular Expression pane. Setting the Match TypeThe buttons at the top of the Rx Toolkit Window determine which function is used to match the regular expression to the search text. The options are based on module-level functions of Python's re module. Choose from the following options:
Adding Modifiers to a Regular ExpressionAdd modifiers to regular expression by selecting one or more of the check boxes to the right of the Regular Expression pane: Note: You must use the Modifiers check boxes to add modifiers to a regular expression. The Rx Toolkit does not recognize modifiers entered in the Regular Expression pane.
Evaluating Regular ExpressionsA debugged regular expression correctly matches the intended patterns and provides information about which variable contains which pattern. If there is a match...
If there is no match...
![]() Match ResultsIf a regular expression collects multiple words, phrases or numbers and stores them in groups, the Match Results pane displays details of the contents of each group. The Match Results pane is displayed with as many as four columns, depending on which match type is selected. The Group column displays a folder for each match; the folders contain numbered group variables. The Span column displays the length in characters of each match. The Value column lists the values of each variable. Modifier ExamplesThis section shows some sample regular expressions with various modifiers applied. In all examples, the default match type (Match All) is assumed: Using Ignore CaseThe Ignore case modifier ignores alphabetic case distinctions while matching. Use this when you do not want to specify the case in the pattern you are trying to match. To match the following test string... Testing123 ...you could use the following regular expression with Ignore case selected: ^([a-z]+)(\d+) The following results are displayed in the Match Results pane: ![]() Discussion This regular expression matches the entire test string. The Using Multi-Line ModeThe Multi-line modifier allows To match the subject part of the following test string... "okay?" ...you could use the following regular expression with Multi-line selected: ^(\"okay\?\") The following results are displayed in the Match Results pane: ![]() Discussion This regular expression matches the entire test string. The Using Single-Line ModeThe Single-line modifier mode allows "." to match newline characters. Use this when a pattern is more than one line long, has at least one newline character, and you want to match newline characters. To match the following test string... Subject: Why did this work? ...you could use the following regular expression with Single-line selected: (:[\t ]+)(.*)work\? The following results are displayed in the Match Results pane: ![]() Discussion This regular expression matches everything in the test string following the word "Subject", including the colon and the question mark. The Using Multi-line Mode and Single-line ModeTo match more of the following test string... Subject: Why did this work? ...you would need both the Multi-line and Single-line modifiers selected for this regular expression: ([\t ]+)(.*)^work\? The following results are displayed in the Match Results pane: ![]() Discussion This regular expression matches everything in the test string following the word "Subject", including the colon and the question mark. The If you used only the Single-line modifier, this match would
fail because the caret " If you used only the Multi-line modifier, this match would
fail because the period " Using VerboseThe Verbose modifier ignores whitespace and comments in the regular expression. Use this when you want to pretty print and/or add comments to a regular expression. To match the following test string... testing123 ...you could use the following regular expression with the Verbose modifier selected: (.*?) (\d+) # this matches testing123 The following results are displayed in the Match Results pane: ![]() Discussion This regular expression matches the entire test string. The Using Regular ExpressionsOnce a regular expression has been built and debugged, you can add it to your code by copying and pasting the regular expression into the Komodo Editor Pane. Each language is a little different in the way it incorporates regular expressions. The following are examples of regular expressions used in Perl, Python, PHP and Tcl. PerlThis Perl code uses a regular expression to match two different spellings of the same word. In this case the program prints all instances of "color" and "colour". while($word = <STDIN>){ print "$word" if ($word =~ /colou?r/i ); } The metacharacter "?" specifies that the preceding character, "u", occurs zero
or one times. The modifier "i" (ignore case) that follows PythonThis Python code uses a regular expression to match a pattern in a string. In
Python, regular expressions are available via the import re m = re.search("Java[Ss]cript", "in the JavaScript tutorial") if m: print "matches:", m.group() else: print "Doesn't match." The TclThis Tcl code uses a regular expression to match all lines in a document that contain a URL. while {[gets $doc line]!=-1} { if {regexp -nocase {www\..*\.com} $line} { puts $line This PHPThis PHP code uses a Perl Compatible Regular Expressions(PCRE) to search for valid phone numbers in the United States and Canada; that is, numbers with a three-digit area code, followed by an additional seven digits. $numbers = array("777-555-4444", "800-123-4567", "(999)555-1111", "604.555.1212", "555-1212", "This is not a number", "1234-123-12345", "123-123-1234a", "abc-123-1234"); function isValidPhoneNumber($number) { return preg_match("/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}$/x", $number); } foreach ($numbers as $number) { if (isValidPhoneNumber($number)) { echo "The number '$number' is valid\n"; } else { echo "The number '$number' is not valid\n"; } } This PHP example uses the RubyThis Ruby code uses a regular expression that does simple email addresses validation. puts 'Enter your email address:' cmdline = gets.chomp addrtest = /^\w{1,30}\@\w{1,30}\.\w{2,4}$/i if addrtest.match(cmdline) puts 'Address is valid.' else puts 'Address is NOT valid.' tries = tries - 1 end The regular expression is stored in the
It will accept some addresses that are not fully RFC compliant, and will not accept long user or domain names. A more robust regular expression is: /^([a-z0-9]+[._]?){1,}[a-z0-9]+\@(([a-z0-9]+[-]?){1,}[a-z0-9]+\.){1,}[a-z]{2,4}$/i This does not limit the length of the username or domain. It also enforces some additional requirements:
|