开发者

Separate a string using a regex

 _, _, XYZ, Path, filename  = string.find("re32:HKEY_LOCAL_MACHINE\\SOFTWARE\\XYZ\\Assistant\\Active ", "(%w+):(.+)\\(.*)")

print(XYZ)  
print(Path)    
print(filename)

The above code outputs:

 re32
 HKEY_LOCAL开发者_如何转开发_MACHINE\SOFTWARE\XYZ\Assistant\
 Active

I need the output in below form--that is, instead of three groups, I need four:

re32
HKEY_LOCAL_MACHINE
SOFTWARE\XYZ\Assistant\
Active

What has to be done in this case?


XYZ, RootKey, Path, filename = ([[re32:HKEY_LOCAL_MACHINE\SOFT WARE\XYZ\Assistant\Active ]]):match ( [[(%w+):([^\]+)(.+)\(.*)]])

Use [[]] instead of "" to stop escape sequences.


_, _, XYZ, RootKey, Path, filename = string.find("re32:HKEY_LOCAL_MACHINE\SOFTWARE\XYZ\Assistant\Active ", "(%w+):(.-)\(.+)\(.*)")

print(XYZ)
print(RootKey)
print(Path)
print(filename)

Should produce

re32

HKEY_LOCAL_MACHINE

SOFTWARE\\XYZ\\Assistant\\

Active


You can have named groups in regular expressions. example:- (/group1\[0-9])(abc) (replace "/" with "<" and "\" with ">" in above example) this regx will match "3abc" and you can get the matched number by choosing the group name(group1) in the match. refeer this http://www.regular-expressions.info/named.html


_, _, XYZ, RootKey, Path, filename = string.find(
    "re32:HKEY_LOCAL_MACHINE\\SOFTWARE\\XYZ\\Assistant\\Active ",
    "(%w+):(.-)\\(.+\\)(.*)")

print(XYZ)
print(RootKey)
print(Path)
print(filename)

output:

re32
HKEY_LOCAL_MACHINE
SOFTWARE\XYZ\Assistant\
Active 

See it on ideone.com

This answer is essentially the same as Serge's, but the backslashes are properly escaped in the target and pattern strings, and the final backslash is included the third capturing group.

But most importantly, this solution is tested. Ideone FTW!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜