Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Using regex to identify strings with capital letters

From   "Beecroft, Erik (VDSS)" <>
To   <>
Subject   st: Using regex to identify strings with capital letters
Date   Thu, 27 May 2010 08:25:31 -0400

Thank you to Martin and Nick for solving my problem, Martin with the
regexm function and Nick with inrange.

Both of these work with my data:
	keep if regexm(substr(var1,1,2) , "^([A-Z][A-Z])")
	keep if inrange(substr(var1,1,1), "A", "Z") &
inrange(substr(var1,2,1), "A","Z")

Both also keep records that begin with "PO" for Post Office box.  These
records I don't want, but easily dropped by modifying Martin's
	drop if regexm(substr(var1,1,2) , "^([P][O])")

Thank you for your help.


Martin is correct that -inrange("Er", "AA", "ZZ")- is true. Possibly
this is Erik's specific problem, namely that having the first capital
letter in "A" ... "Z" is necessary but not sufficient. 

I offer as a stronger criterion 

inrange(substr(myvar,1,1), "A", "Z") & inrange(substr(myvar,2,1), "A",

I continue to like regex solutions when they are the simplest available!


*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index