我正在尝试匹配字符串中同一模式的多次出现。不幸的是,仅使用ustrregexs
和ustrregexm
返回第一个匹配项。另外,我不知道可能有多少个匹配项,因此硬编码n
匹配项不是一种选择。有没有办法找到Stata中的所有比赛?
例子:
clear all
input x str250 y
1 "123 12"
2 "345 678"
3 "000 000 000"
4 "111"
5 "00"
6 "000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000000 000 000"
end
* Returns only the first match
gen match = ustrregexs(0) if ustrregexm(y, "(\d{3})+")
moss
来自SSC的专门致力于这个问题。如果“本地”排除社区贡献的命令,那么你需要编写自己的代码。
clear all
input x str20 y
1 "123 12"
2 "345 678"
3 "000 000 000"
4 "111"
5 "00"
end
moss y, match("([0-9][0-9][0-9])") regex
list
+--------------------------------------------------------------------------------+
| x y _count _match1 _pos1 _match2 _pos2 _match3 _pos3 |
|--------------------------------------------------------------------------------|
1. | 1 123 12 1 123 1 . . |
2. | 2 345 678 2 345 1 678 5 . |
3. | 3 000 000 000 3 000 1 000 5 000 9 |
4. | 4 111 1 111 1 . . |
5. | 5 00 0 . . . |
+--------------------------------------------------------------------------------+