👇 连享会 · 推文导航 |
www.lianxh.cn
作者:
连玉君 (中山大学)
邮箱:
[email protected]
温馨提示:
文中链接在微信中无法生效。请点击底部
「阅读原文」
。或直接长按/扫描如下二维码,直达原文:
目录
-
1. 问题背景
-
2. 例子
-
3. 总结
-
4. 补充说明
-
5. 相关推文
1. 问题背景
处理文字变量时,经常找出包含特定关键词的观察值。若希望查询过程中对英文字母的大小写不敏感,可以使用
ustrregexm()
字符函数,只需
ustrregexm(var, key, 1)
将第三个填项设定为 1 即可。
后文介绍的几个字符函数,都可以在
[FN]
string_functions
(网页版) 查到其语法和使用说明。
2. 例子
我们先手动生成一份虚构数据:
clear
input str10 car
"Audi"
"audi"
"AUDI"
"Audi-Q7"
"TT-AuDI"
end
接下来,对比一下
strpos()
,
ustrregexm()
等函数的差异:
local key "Audi"
// local key "audi"
gen strpos = strpos(car, "`key'")
gen strmatch = strmatch(car, "`key'")
gen ustr_m0 = ustrregexm(car, "`key'")
gen ustr_m1 = ustrregexm(car, "`key'", 1)
gen regexm = regexm(car, "[Aa][Uu][Dd][Ii]")
list , clean noobs
结果如下:
. list , clean noobs
car strpos strmatch ustr_m0 ustr_m1 regexm
Audi 1 1 1 1 1
audi 0 0 0 1 1
AUDI 0 0 0 1 1
Audi-Q7 1 0 1 1 1
TT-AuDI 0 0 0 1 1
在最后一列中,虽然我们使用
regexm()
函数也可以达到相同的效果,但需要耐心设定正则表达式,故而缺乏通用性。
3. 总结
目前看来,若需在字符串查询或匹配时忽略英文字符大小写差异,使用
ustrregexm(s,re[,noc])
(
[
FN]
f_ustrregexm
) 是最佳选择。
4. 补充说明
Note: 本文中的帮助文件链接可以使用
ihelp
命令快捷获取:
*-安装
ssc install ihelp, replace // 若已安装,请跳过此行
产生 PDF 帮助文件链接
[FN]
string_functions
和
网页版
的命令如下:
. ihelp string function, m
[**[FN]** string_functions](https://www.stata.com/manuals/fnstringfunctions.pdf)
. ihelp string function, m web
[**[FN]** string_functions](https://www.stata.com/help.cgi?string_functions)
产生 HTML 帮助文件链接
[FN]
f_ustrregexm
的命令如下:
. ihelp f_ustrregexm, m web
[**[FN]** f_ustrregexm](https://www.stata.com/help.cgi?f_ustrregexm)
5. 相关推文
Note:产生如下推文列表的 Stata 命令为:
lianxh 字符 正则
安装最新版
lianxh
命令:
ssc install lianxh, replace
-
-
连玉君, 2021,
Stata错误信息-stacktracenotavailable:安装路径中不要包含中文字符和空格
, Blog No.539.
-
连玉君, 2020,
字符编码问题三个不可见的字符(0xEF-0xBB-0xBF,即BOM)
, Blog No.423.
-
-
李依, 2022,
Stata数据处理:将字符变量编码为数值变量-encoder
, Blog No.950.
-
陈勇吏, 2020,
七条建议:用Stata处理文字变量和字符变量
, Blog No.279.
-
林佳辉, 2020,
Stata数据处理:字符型日期变量的转换
, Blog No.319.
-
-
梁淑珍, 2022,
Stata:正则表达式教程
, Blog No.968.
-
游万海, 2021,
用正则表达式整理文献:正文与文末一一对应
, Blog No.547.
-
游万海, 2020,
Stata爬虫-正则表达式:爬取必胜客
, Blog No.287.
-
连享会, 2020,
在VisualStudio(vsCode)中使用正则表达式
, Blog No.10.
-
游万海, 2020,
Stata:正则表达式和文本分析
, Blog No.35.
-
连享会, 2020,
正则表达式语言-快速参考
, Blog No.81.