Chet
Chet

Reputation: 193

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?

Upvotes: 1

Views: 526

Answers (4)

daurnimator
daurnimator

Reputation: 4311

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

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

Upvotes: 1

Alan Moore
Alan Moore

Reputation: 75272

_, _, 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!

Upvotes: 0

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21376

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

Upvotes: 0

Sergey
Sergey

Reputation: 1173

_, _, 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

Upvotes: 0

Related Questions