我已经在这方面工作了太久了,我似乎无法想出一个能解决这个问题的正则表达式。我知道我可以使用其他编码语言来迭代字符,但我只是想用Stata来做,而不必进入R或Python。这是:
我尝试解析的字符串通常具有如下设置:
Name (Type / $## Million / #### )
有时缺少最后的结束括号,但如果是,则最后一个字符是字符串的结尾。我希望能够匹配括号内容,但问题是有时Name包含括号,比如
Bank (other) (... / ... / ...)
此外,有时Type也有括号,例如
Name (Loan (other) / ... / ...)
基本思想是我正在寻找包含由其他字符分隔的两个正斜杠的括号集的内容。知道怎么做吗?
到目前为止,我提出的最好的是:
\(([^\)]*\/[^\)]*\/.*\)?)$
但是当我想要抓取的集合中有一个括号 时会遇到问题。任何帮助将不胜感激。以下是一些示例行。应将每一行视为新字符串。
IFC (Equity / $12 Million / 1993
IFC (Equity / $28 Million / 1995)
IFC (Loan / $30 Million / 1995
IFC (Syndication / $40 Million / 1995)
BOAD (Loan / $7 Million / 1995
IFC (Equity / $5 Million / 1997)
IFC (Loan / $13 Million / 1997
MIGA (Guarantees Only) (Guarantee / $30 Million / 1995)
IFC (Equity / $2 Million / 1997
IFC (Syndication / $3 Million / 1997
IFC (Equity / $1 Million / 1998
IFC (Syndication / $12 Million / 1998
IFC (Quasi-equity / $7 Million / 1998
IFC (Risk Management (including Political Risk Insurance) / $1 Million / 1994)
答案 0 :(得分:2)
我让你表达了挫折感:通常它会被编辑为与技术问题无关,但在这里寻找一个太复杂的解决方案是问题的一部分。我经常看到人们在应用基本字符串命令时会注意搜索类似Grail的正则表达式,并且函数会破解他们的问题。
这是一种方式。对于字符串的一些进一步编辑似乎可能是split
再次和subinstr()
是选择的工具。
clear
input str80 mydata
"IFC (Equity / $12 Million / 1993"
"IFC (Equity / $28 Million / 1995)"
"IFC (Loan / $30 Million / 1995"
"IFC (Syndication / $40 Million / 1995)"
"BOAD (Loan / $7 Million / 1995"
"IFC (Equity / $5 Million / 1997)"
"IFC (Loan / $13 Million / 1997"
"MIGA (Guarantees Only) (Guarantee / $30 Million / 1995)"
"IFC (Equity / $2 Million / 1997"
"IFC (Syndication / $3 Million / 1997"
"IFC (Equity / $1 Million / 1998"
"IFC (Syndication / $12 Million / 1998"
"IFC (Quasi-equity / $7 Million / 1998"
"IFC (Risk Management (including Political Risk Insurance) / $1 Million / 1994)"
end
split mydata, parse(/)
rename (mydata?) (what howmuch when)
destring when, ignore(")") replace
list what how when
+------------------------------------------------------------+
1. | what |
| IFC (Equity |
|------------------------------------------------------------|
| howmuch | when |
| $12 Million | 1993 |
+------------------------------------------------------------+
+------------------------------------------------------------+
2. | what |
| IFC (Equity |
|------------------------------------------------------------|
| howmuch | when |
| $28 Million | 1995 |
+------------------------------------------------------------+
+------------------------------------------------------------+
3. | what |
| IFC (Loan |
|------------------------------------------------------------|
| howmuch | when |
| $30 Million | 1995 |
+------------------------------------------------------------+
+------------------------------------------------------------+
4. | what |
| IFC (Syndication |
|------------------------------------------------------------|
| howmuch | when |
| $40 Million | 1995 |
+------------------------------------------------------------+
+------------------------------------------------------------+
5. | what |
| BOAD (Loan |
|------------------------------------------------------------|
| howmuch | when |
| $7 Million | 1995 |
+------------------------------------------------------------+
+------------------------------------------------------------+
6. | what |
| IFC (Equity |
|------------------------------------------------------------|
| howmuch | when |
| $5 Million | 1997 |
+------------------------------------------------------------+
+------------------------------------------------------------+
7. | what |
| IFC (Loan |
|------------------------------------------------------------|
| howmuch | when |
| $13 Million | 1997 |
+------------------------------------------------------------+
+------------------------------------------------------------+
8. | what |
| MIGA (Guarantees Only) (Guarantee |
|------------------------------------------------------------|
| howmuch | when |
| $30 Million | 1995 |
+------------------------------------------------------------+
+------------------------------------------------------------+
9. | what |
| IFC (Equity |
|------------------------------------------------------------|
| howmuch | when |
| $2 Million | 1997 |
+------------------------------------------------------------+
+------------------------------------------------------------+
10. | what |
| IFC (Syndication |
|------------------------------------------------------------|
| howmuch | when |
| $3 Million | 1997 |
+------------------------------------------------------------+
+------------------------------------------------------------+
11. | what |
| IFC (Equity |
|------------------------------------------------------------|
| howmuch | when |
| $1 Million | 1998 |
+------------------------------------------------------------+
+------------------------------------------------------------+
12. | what |
| IFC (Syndication |
|------------------------------------------------------------|
| howmuch | when |
| $12 Million | 1998 |
+------------------------------------------------------------+
+------------------------------------------------------------+
13. | what |
| IFC (Quasi-equity |
|------------------------------------------------------------|
| howmuch | when |
| $7 Million | 1998 |
+------------------------------------------------------------+
+------------------------------------------------------------+
14. | what |
| IFC (Risk Management (including Political Risk Insurance) |
|------------------------------------------------------------|
| howmuch | when |
| $1 Million | 1994 |
+------------------------------------------------------------+