Home > FAQ > Useful Basic Tasks when writing a script > How do I find the window which has the focus?

Using Regular Expressions

A regular expression is a set of pattern matching rules encoded in a string according to certain syntax rules. The syntax is a bit complex but it allows much more useful pattern matching than say simple wildcards like ? and *. Regular expressions are supported in UseWindowRegEx and CloseWindowRegEx functions to match a window name specified by such a regular expression. The regular expression serves as a template for matching a character pattern to the window name that is being searched. Regular expressions consist of literal symbols and special combinations of characters, named tokens.

The most common cases for Regular Expressions are listed in the table below:

Token  Description  Examples 
 .  Matches a single character  a.r matches a0r, air, a£r,...
 *  Repeats the previous character zero or several times  abc* matches ab, abc, abcc, abccc, ...
 +  Repeats the previous character one or several times  abc+ matches abc, abcc, abccc, ...
 ?  Makes the preceding character optional  abc? matches ab or abc
 |  Alternation, equivalent of OR  200[6|7] matches 2006 or 2007
 []  Matches one out of several characters  gr[ae]y matches gray, grey but not graey
 {n}  Repeats n times the previous character  a{3} matches aaa
 {n,m}  Repeats the previous character between n and m times  a{2,4} matches aa, aaa, aaaa
 [ - ]  Matches one out of a range of several characters  gr[0-9]y matches gr0y, gr1y, ... gr9y but not gr19y
 [^ - ]  Excludes the range of several characters  gr[^0-9]y matches gray, grby, grcy, ... grzy but not gr0y, gr1y,... gr9y
 ^  Outside a [, matches at the start of the string  ^begin matches begin, beginning, begin with
 $  Matches at the end of the string  end$ matches end, will end, at the end
 \character  A backslash escapes special characters above their meaning  \+ matches + (and so the + sign does not have its special  meaning)


See also

UseWindowRegEx
CloseWindowRegEx

Examples

'Single character
'Keys have to be sent to the notepad window whatever character is after Doc
#UseExact=1
UseWindowRegEx("NOTEPAD.EXE|Edit|Doc. - Notepad|1")
SendKeys("Hello")
'Triggers for any window Doca, Docb, ..., Doc0, Doc1, ....


'Single character in a list
'Keys have to be sent to the notepad window if, after Doc, the year is 2007, 2008 or 2009
#UseExact=1
UseWindowRegEx("NOTEPAD.EXE|Edit|Doc200[789] - Notepad|1")
SendKeys("Hello")
'Triggers for any window Doc2007, Doc2008, or Doc2009

'Single character NOT in a list
'Keys have to be sent to the notepad window if, after Doc, the year is NOT 2005, 2006 or 2007
#UseExact=1
UseWindowRegEx("NOTEPAD.EXE|Edit|Doc200[^567] - Notepad|1")
SendKeys("Hello")
'Triggers for any window Doc2000, Doc2001, Doc2002, Doc2003, Doc2004, Doc2008, Doc2009 and Doc200a, Doc200b, ...

'Single character within a range
'Keys have to be sent to the notepad window if, after Doc, it can be any year in the 1990s
#UseExact=1
UseWindowRegEx("NOTEPAD.EXE|Edit|Doc199[0-9] - Notepad|1")
SendKeys("Hello")
'Triggers for any window Doc1990, Doc1991,..., Doc1999