bsd bsd - 7 months ago 20
Perl Question

How to perl regex ldap values?

I need to extract two values from given output.

kat\, mith(mkat),OU=Site-SJN,OU=Accounts,OU


I am looking for these two output values

$user = kat\, mith(mkat)
$site = Site-SJN


I've tried these options. Please suggest to get above two values.

$user =~ s/^.*?://;
$user =~ s/^*.?OU=//;
$site =~ s/^Site?/;

Answer

You give only the barest of information, so I can only tell you what will work for that one line of input data

Here's my best guess about what will work for you, but I am pretty certain you're going to come back with another line that doesn't do quite what you want

use strict;
use warnings qw/ all FATAL /;
use feature 'say';

my $s = 'kat\, mith(mkat),OU=Site-SJN,OU=Accounts,OU';

my ($user) = $s =~ /^([^,]*,[^,]*)/;
my ($site) = $s =~ /\bOU=(Site[^,]*)/;

say $user;
say $site;

output

kat\, mith(mkat)
Site-SJN