*! version 1.0 07may2021

findregex

findregex -- Find variable names matching a regular expression pattern

Syntax

findregex
[varlist] , re(["]pattern["])
[options]

Selection
re(["]pattern["]) regular expression pattern

Control
sensitive performs case-sensitive pattern matching

pattern is a string containing a valid regular expression pattern and should be enclosed in quotes (see ustrregexm()).

Description

findregex lists variable names that match a regular expression based on the variables in the current frame. The variable names are listed back to the user. In addition, the matching list of variables is left behind in s(varlist).

The default is to use all variable names in the current dataset. If varlist is provided, then the matching takes place on this subset.

Options

Selection

re(["]pattern["]) a string containing the regular expression pattern and should be quoted.

Control

sensitive performs case-sensitive pattern matching. The default is to perform case-insensitive pattern matching.

Examples

Search all variables, ignoring case, beginning with the letter "m".

. findregex, re("^m")

Search all variables, respecting case, which contain either an "m" or "b" followed by one or two digits in the range of 1 to 4.

. findregex, re("[bw][1-4]{1,2}") sensitive

Search all variables, ignoring case, which contains an American or British spelling of centre.

. findregex, re("cent(re|er)")

Author

Leonardo Guizzetti

London, Ontario

leonardo.guizzetti@gmail.com

Acknowledgements

The idea to use regular expressions was inspired by a post on the Stata Forum. The creation of this command was encouraged by Nicholas J. Cox.

Also see

Article: Stata Journal, volume 20, number 2: dm0048_4

Manual: [D] ds

Help: [D] ds, findname