开发者

Regex find text between

开发者 https://www.devze.com 2023-03-30 15:46 出处:网络
I need a regular expression that will find me the following text: \"postmaster\"=\"infonl@bostik.com\"

I need a regular expression that will find me the following text:

"postmaster"="infonl@bostik.com"
"info"="infonl@bostik.com"
"nobody"="root"

from an input like:

[HKEY_LOCAL_MACHINE\SOFTWARE\Ipswitch\IMail\Domains\bostik.nl\Users\_aliases]
"postmaster"="infonl@bostik.com"
"info"="infonl@bostik.com"
"nobody"="root"

[HKEY_LOCAL_MACHINE\SOFTWARE\Ipswitch\IMail\Domains\bostikfindley.nl]

So I want it to grab everything from \_alisases] until the '[' from the next [HKEY_LOCAL_MACHINE if that's possible. I will use this to output all the variable texts under this node in a text search pro开发者_StackOverflow社区gram.

It's a big file with 18000 lines where I will search in using text search. There are also line which don't include the _aliases] they shouldn't be returned as valid.

Example:

[HKEY_LOCAL_MACHINE\SOFTWARE\Ipswitch\IMail\Domains\bostik.nl\Users\_aliases]
"postmaster"="infonl@bostik.com"
"info"="infonl@bostik.com"
"nobody"="root"

[HKEY_LOCAL_MACHINE\SOFTWARE\Ipswitch\IMail\Domains\bostikfindley.nl]
"Address"="$virtual291"
"TopDir"="D:\\IMail\\bostikfindley_nl"
"Flags"=dword:00000000
"NotifyAddress"=""
"SubMailboxCreate"=dword:00000000
"NotifyPercent"=dword:00000000
"MaxOutboundSize"=dword:00000000
"VirusScan"=dword:00000001
"MaxSize"=dword:00000000
"MaxMsgs"=dword:00000000
"MaxRcv"=dword:00000000
"MaxUsers"=dword:00000000
"UserCount"=dword:00000001
"EnableSSL"=dword:00000001
"ForceSSL"=dword:00000000
"IcalEnable"=dword:00000001

I want only to be returned:

"postmaster"="infonl@bostik.com"
"info"="infonl@bostik.com"
"nobody"="root"

This does have to be a regex i can use in Search and Replace tool to find the information I need.


As you didn't specified a programming language, here is a Perl script that do the job:

#!/usr/local/bin/perl 
use strict;
use warnings;

while(<DATA>) {
    if (/_aliases\]/ ... /\[HKEY/) {
        print unless /\[/;
    }
}

__DATA__
[HKEY_LOCAL_MACHINE\SOFTWARE\Ipswitch\IMail\Domains\bostik.nl\Users\_aliases]
"postmaster"="infonl@bostik.com"
"info"="infonl@bostik.com"
"nobody"="root"

[HKEY_LOCAL_MACHINE\SOFTWARE\Ipswitch\IMail\Domains\bostikfindley.nl]
"Address"="$virtual291"
"TopDir"="D:\\IMail\\bostikfindley_nl"
"Flags"=dword:00000000
"NotifyAddress"=""
"SubMailboxCreate"=dword:00000000
"NotifyPercent"=dword:00000000
"MaxOutboundSize"=dword:00000000
"VirusScan"=dword:00000001

output

"postmaster"="infonl@bostik.com"
"info"="infonl@bostik.com"
"nobody"="root"


I would do a string replace on \[.*?\] with an empty string, the remaining string is your result.

EDIT: oke apparently, you meant something else...

if you only want those 3 attributes try this regex: \"(postmaster|info|nobody)\"\=\".+?\"


if ($subject =~ m/"(.*)\[.*/s) 
$result = $1;

This works with perl. I am unaware of which language you are going to use so it may differ.

Edit : I saw you changed you original post so I will have to reconsider.

Edit 2 : It works with your example.

0

精彩评论

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