Article Preview
Buy Now
COLUMN
Speaking On Condition
Conditionals In Regular Expressions
Issue: 12.5 (September/October 2014)
Author: Kem Tekinay
Author Bio: Kem Tekinay is a Macintosh consultant and programmer who started with Xojo when it was still REALbasic. He is the author of RegExRX (
Article Description: No description available.
Article Length (in bytes): 12,659
Starting Page Number: 84
Article Number: 12514
Related Web Link(s):
http://www.mactechnologies.com/index.php
Excerpt of article text...
Could you write a method without using an
If
statement? Well, you could, if it had very limited scope, and often you do. But can you imagine writing an entire application like that? Well, you could do that too, if it were a one-trick-pony-type program. The classic "Hello World" needs to make no decisions of any kind, but something more complex must take conditions into account all the time.And so it is with regular expressions. Usually your needs are pretty straightforward and you can create even complex patterns where the only conditions that need be considered are in the text itself. Regular expressions are one big conditional, after all, since every token will match—or not—depending on the source text.
But sometimes you will need to make more advanced decisions based on whether a section of the pattern matched, or even what was matched. Naturally, there are structures for this purpose.
Alternator
The simplest type of conditional is alternation, covered briefly in the previous column on Subgroups. Strictly speaking, this is not really a conditional, but it does provide a simple way to make a decision and is relevant to the usage of true conditionals.
When any one of multiple choices will fit your pattern, you use an alternator. It's an
Or
statement against your text where the this or that (or the other) will match. For example, suppose you are trying to match entries in a log file, but are only interested in certain months. If the dates are in the form of YYYY-MM-DD, and you only want February and March entries, the pattern would use the alternation token, the vertical bar, to list the choices.
...End of Excerpt. Please purchase the magazine to read the full article.