Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How-To: Regular Expressions Explained
Dean Roddey Wrote:In a nutshell, it comes down to a sequence of characters that are used to match a pattern. It's composed of one of a set of components:

1. Characters. Just regular characters to be matched

ABC means match A, then B, then C

2. Any single character is represented by a period, so:

A.C will match A1C, A2, A4C, AxC and so forth

3. One of a set of things, divided by a | character, so:

A|B will match a single A or single B.
(A|B)(C|D) will match AC, AD, BC, or BC

4. Repetition indicators. ?=One or zero, *=Zero or more, +=One ormore

AB?C will match ABC or AC, since one or zero Bs can be between them
AB+C will match ABC, ABBC, ABBBBBBC, and so on, because one or more Bs are legal
AB*C will match AC, ABC, ABBBBC, and so on, because zero or more Bs are legal.

5. Range indicators, using []

[A-Z] will match any letter from capital A to capital Z
[0-9] will match any digit
[123] will match a one, two or three (so it's kind of like (1|2|3) really, but just demonstrating that it's not necessarily a range like the first two.)

You can mix these things together in many ways, particularly by using the parenthesis, because the repetitions apply to the whole parethesized previous section if it is parenthesized, or to the range.

(A|B)+CD will match ACD, BCD, ABCD, AAAABBBBCD and so forth, because it's one or more As or Bs followed by CD.

One|Two will match either One or Two.

(Two|Four) (Rabbits|Foxes) will match "Two Rabbits", "Four Rabbits", "Two Foxes" or "Four Foxes".

[0-9]* will match any number of digits (ncluding zero digits)

[0-9]+Db will match 0Db, 0132Db and so forth, i.e. one or more digits followed by Db.

(1|2)[5-9][A-F] would match 15A, 29C, and so forth. It's 1 or 2, followed by one digit from 5 to 9, followed by one character from A to F.

[^ACD]+ means one or more characters, none of which can be A, C, or D.

A.*B would mean A followed by zero or more of any character, followed by a B.

Of course you have to escape the magic characters to get them to be treated as regular characters. So:

\[\(\* would match [(* because they are all escaped. \\ represents a single \ character, since you have to escape the escape character as well.

That's it in a nutshell. There are a few more aspects to it, but that's the most important stuff. You can write quit elaborate ones, but they can be hard to prove correct just by looking at them.

And you cannot use regular expressions to count things. So there's no regular expression that says "four A, B, or C characters.". You would hav eto just do:


And explicitly indicate all four options.
Mykel Koblenz
Illawarra Smart Home
Regular Expressions, regex, is an incredibly powerful tool for pattern matching.

Here are some internet based resources:

A Google search will get you a wealth of info...
I'm trying to use a RegEx to determine if I need to execute a trigger. It's not working, clearly PBKAC.

Specifically, if I set a variables driver field to AtticEquip(number), ie AtticEquip98.30 , I want this trigger to fire.

I use "Is New Fld Value For", then for CompVal I use AtticEquip* and checked the RegEx box.

I tried a few other things, but clearly not sure what as it's not working. Any ideas?
Some of my devices: Sonos, Aeotec zWave, Nest, Rain8Net, Various H/T
What's next: CQC-Voice, Brultech GEM
My vlogs:
You need to make it dot asterisk. Asterisk means zero or more, of the thing just before it. So you are saying something that ends with zero or more p's. If you do this:


The dot means any character. So that would be anything that ends with zero of more of any characters.
Dean Roddey
Explorans limites defectum
BTW, the moniker I think will be part of the field name, so you'd want to include that as well, so:


I think would be the appropriate thing.
Dean Roddey
Explorans limites defectum

Forum Jump:

Users browsing this thread: 1 Guest(s)