I'm still pretty new to perl and regex and need some help getting started. I would love to provide some code, but that's kinda where I'm stuck.
What I'm trying to do is that I have this string in a file like this:
dn: CN=doe\, john,OU=Users,DC=domain,DC=com
and a string like this:
uid: d12345
I need to do a search and replace to get the following result.
dn: uid= d12345,OU=Users,DC=domain,DC=com
Can anyone help me get started with this one? Much thanks!
So you want to replace CN=doe\, john with uid= d12345? Try this:
$uidString = "uid: d12345";
$dnString = "dn: uid= d12345,OU=Users,DC=domain,DC=com";
if( $uidString =~ /uid: (\w+)/ ) {
$uid = $1;
$dnString =~ s/CN=.+?[^\\],/uid= $uid,/;
}
That will replace everything from CN= to the first unescaped comma with the uid.
Won't a one line regex do the trick?
use strict;
use warnings;
my $a = "dn: CN=doe\, john,OU=Users,DC=domain,DC=com";
my $b= "uid: d12345";
#the regex
$a =~ s/CN(.*?), .*?,/$b,/;
print "$a";
I suspect your DNs and uids will be dynamic. Here is something that will help. The regex will substitute CN= all the way until the comma with whatever string you put in $uid.
#!/usr/bin/env perl
use strict;
use warnings;
my $string = 'dn: CN=doe\, john,OU=Users,DC=domain,DC=com';
my $uid_str = 'uid: d12345';
my ($uid) = $uid_str =~ m/^uid:(.+)$/;
$string =~ s/CN=.+(,OU=.+$)/uid=$uid$1/;
print "String is: $string\n";
Output: String is: dn: uid= d12345,OU=Users,DC=domain,DC=com
Related
I have written a basic program using regular expression.
However the entire line is being returned instead of the matched part.
I want to extract the number only.
use strict;
use warnings;
my $line = "ABMA 1234";
$line =~ /(\s)(\d){4}/;
print $line; #prints *ABMA 1234*
Is my regular expression incorrect?
If you want to print 1234, you need to change your regex and print the 2nd match:
use strict;
use warnings;
my $line = "ABMA 1234";
$line =~ /(\s)(\d{4})/;
print $2;
You can replace the exact value with the corresponding values. And your are not removing the text \w;
use strict;
use warnings;
my $line = "ABMA 1234";
$line=~s/([A-z]*)\s+(\d+)/$2/;
print $line; #prints only 1234
If you want to store the value in the new string then
(my $newstring = $line)=~s/([A-z]*)\s+(\d+)/$2/;
print $newstring; #prints only 1234
Just try this:
I don't know how you output the match in perl but you can use below regex for output the full match in your regex, you might getting space appended with your result in your current regex.
\b[\d]{4}
DEMO
I need to do pattern match with two variables one contains the string and the other contains the regex pattern
I tried with the following program
#!/usr/bin/perl
my $name = "sathish.java";
my $other = '*.java';
if ( $name =~ m/$other/ )
{
print "sathish";
}
kindly help where am missing
Thanks
Sathishkumar
#Shmuel answer suits your needs, but if you are looking for common way of extract the filename from a complete path name, you can use File::Basename:
use strict;
use warnings;
use File::Basename;
my ($name, $path, $suffix) = fileparse("/example/path/test.java", qw/.java/);
print "name: $name\n";
print "path: $path\n";
print "suffix: $suffix\n";
it prints:
name: test
path: /example/path/
suffix: .java
'*.java' is not a valid regex. you probably want to use this code:
my $other = '\.java$';
if ($name =~ m/$other/) {
you can use following style which is more appropriate of your need
$other = "*.java";
if ($name =~m/^$other/){}
--SJ
I like Shmuel's answer, but I'm guessing you probably want to capture the first part of the regex into variable as well?
if so, use
my $other = '\.java$';
if ($name =~ m/(\D*)$other/) {
print $1;
# prints "sathish"
}
I have an odd situation where I want to remove all but the first match of a substring inside of a very long undelimited string. I have found some similar topics here, but none quite like mine.
For simplicities sake, here are some sudo before and after strings.
I have an undelimited file where "c" could be thousands of random characters but "bbb" is a unique string:
aaabbbbbbccccccbbbccccccbbbccccccaaa
I want to remove all but the first bbb:
aaabbbccccccccccccccccccaaa
Also, I would like to be able to use this as a perl script I can pipe through:
cat file.in | something | perl -pe 's/bbb//g' | somethingelse > file.out
But, unlike my example above, I want to leave the first occurrence of "bbb" intact."
This seems like it should be fairly easy, but it is stumping me.
Any ideas?
Thanks in advance!
Perhaps the following will be helpful:
use strict;
use warnings;
my $string = 'aaabbbbbbccccccbbbccccccbbbccccccaaa';
$string =~ s/(?<=bbb).*?\Kbbb//g;
print $string;
Output:
aaabbbccccccccccccccccccaaa
my $string = 'aaabbbbbbccccccbbbccccccbbbccccccaaa';
my $seen;
sub first {
$seen++;
return $_[0] if $seen eq 1;
return '';
}
$string =~ s/(bbb)/first($1)/ge;
say $string;
Outputs:
aaabbbccccccccccccccccccaaa
I want to search a sentence and replace it with the value of hash that matched string like this:
my $sentence = "abc def hello hi";
$sentence =~ s/abc def/$hash{'abc def'}/g;
I am not getting the correct output.
Can any one please help me?
This works for me:
#!/usr/bin/env perl
use strict;
use warnings;
my %hash = ( 'abc def' => 'pqr xyz' );
my $sentence = "abc def hello hi";
$sentence =~ s/abc def/$hash{'abc def'}/g;
print "$sentence\n";
When run, it prints:
pqr xyz hello hi
If that's not what you expected, what were you expecting? (Note that there were a number of typos in the original version of the Perl code in the question; I assumed they were incidental, but maybe they were key to your problem. Using use strict; and use warnings; helps spot problems such as misspelled variable names.)
I need to write regular expression that will parse strings like this:
Build-Depends: cdbs, debhelper (>=5), smthelse
I want to extract package names (without version numbers and brackets).
I wrote something like this:
$line =~ /^Build-Depends:\s*(\S+)\s$/
But it's not exactly what I want.
Does someone know how to manage it?
P.S. I just want to get the list: "cdbs debhelper smthelse" as a result
This regex should do what you want: /\s(\S*)(?:\s\(.*?\))?(?:,|$)/g
Edit: You'd call it like this to loop through all the results:
while ($str =~ /\s(\S*)(?:\s\(.*?\))?(?:,|$)/g) {
print "$1 is one of the packages.\n";
}
With your regex /^Build-Depends:\s*(\S+)\s$/ you are matching until the end of string.
Try /^Build-Depends:\s*(\S+)\s/ instead.
This will work for the types of package names listed here.
use warnings;
use strict;
my #packs;
my $line = "Build-Depends: cdbs, debhelper (>=5), smthelse";
if ( $line =~ /^Build-Depends: (.+)$/ ) { # get everything
#packs = split /,+\s*/, $1;
s/\([^)]+\)//g for #packs; # remove version stuff
}
print "$_\n" for #packs;
How about splitting the input on whitespace and print each element if a ( is not present?
Something like this perhaps
perl -lane 'foreach $_ (#F[1..scalar(#F)]) {print if not m/\(/}'
cdbs,
debhelper
smthelse