#! /usr/bin/perl
$str = "ab_cde,efg_gh,drg_fgt,main_xx,sum(abc),avg(def)";
or
$str = "ab_cde,bc_bn,gy_ihf,efg_gh,drg_fgt,main_xx,sum(abc),avg(def)";
Guys, the string before main_xx is dynamic means there can be more elements with this format like xx_xx or xxx_xx or xx_xxx or xxx_xxxx or it can be as many characters before and after "underscore". So before main_xx, as many elements can come with above format. I want to match string UP TO main_xxbecause even fetching dynamically, this "main_xx" will be the last element and want to ignore elements aftermain_xx`. Please help to create a regex for this.
#!/usr/bin/perl -w
use strict;
my $str = "ab_cde,efg_gh,drg_fgt,main_xx,sum(abc),avg(def)";
(my $result) = ($str =~ m/(.*main_xx)/);
print $result;
The output will be everything up to main_xx (given xx is just the string made of x's).
Try this
my $str = "ab_cde,efg_gh,drg_fgt,main_xx,sum(abc),avg(def)";
my ($match)= $str =~m/(.+main_xx)/;
print $match;
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 am trying to simultaneously remove and store (into an array) all matches of some regex in a string.
To return matches from a string into an array, you could use
my #matches = $string=~/$pattern/g;
I would like to use a similar pattern for a substitution regex. Of course, one option is:
my #matches = $string=~/$pattern/g;
$string =~ s/$pattern//g;
But is there really no way to do this without running the regex engine over the full string twice? Something like
my #matches = $string=~s/$pattern//g
Except that this will only return the number of subs, regardless of list context. I would also take, as a consolation prize, a method to use qr// where I could simply modify the quoted regex to to a sub regex, but I don't know if that's possible either (and that wouldn't preclude searching the same string twice).
Perhaps the following will be helpful:
use warnings;
use strict;
my $string = 'I thistle thing am thinking this Thistle a changed thirsty string.';
my $pattern = '\b[Tt]hi\S+\b';
my #matches;
$string =~ s/($pattern)/push #matches, $1; ''/ge;
print "New string: $string; Removed: #matches\n";
Output:
New string: I am a changed string.; Removed: thistle thing thinking this Thistle thirsty
Here is another way to do it without executing Perl code inside the substitution. The trick is that the s///g will return one capture at a time and undef if it does not match, thus quitting the while loop.
use strict;
use warnings;
use Data::Dump;
my $string = "The example Kenosis came up with was way better than mine.";
my #matches;
push #matches, $1 while $string =~ s/(\b\w{4}\b)\s//;
dd #matches, $string;
__END__
(
"came",
"with",
"than",
"The example Kenosis up was way better mine.",
)
I am matching a string of the form A<=>B!C<=>D!E<=>F... and want to do checks on the letters. Basically I want to tell if the letters are in the class according to a hash I have defined. I had the idea of doing the following regex and then looping through the matched strings:
$a =~ /(.)<=>(.)/g;
But I can't figure out to tell how many $1, $2 variables have matched. How do I know how many there are? Also, is there a better way to do this? I am using Perl 5.8.8.
You'll want the 'countof' operator to count the number of matches:
my $count = () = $string =~ /(.)<=>(.)/g;
Replacing the empty list with an array will retain the matches:
my #matches = $string =~ /(.)<=>(.)/g;
Which provides another way to get the $count:
my $count = #matches; # scalar #matches works too
Use a while loop
use warnings;
use strict;
my %letters = map { $_ => 1 } qw(A C F);
my $s = 'A<=>B!C<=>D!E<=>F';
while ($s =~ /(.)<=>(.)/g) {
print "$1\n" if exists $letters{$1};
print "$2\n" if exists $letters{$2};
}
__END__
A
C
F
Create a variable and increment it each time you go through your loop?
If there are more than 2 characters
"Hiiiiiii
My frieeend!!!!!!!"
I need to be reduced to
"Hii
My frieend!!"
Please undestand that in my language there are many words with double chars.
Thnx in advance
kplla
Perl / regex (and if it's not english, Perl has given me better luck with Unicode than PHP):
#!/usr/bin/perl
$str = "Hiiiiii My Frieeeeend!!!!!!!";
$str =~ s/(.)\1\1+/$1$1/g;
print $str;
If a PHP and regex based solution is fine you can do:
$str = "Hiiiiiii My frieeend!!!!!!!";
$str = preg_replace('#(.)\1+#','$1',$str);
echo $str; // prints Hi My friend!
$str = preg_replace('#(.)\1{2,}#','$1$1',$str);
echo $str; // prints Hii My frieend!!
You can make use of the regex used above in Perl too:
$str = "Hiiiiiii My frieeend!!!!!!!";
$str =~s/(.)\1{2,}/$1$1/g;
Here's another regex solution that uses lookahead (just for fun), in Java:
System.out.println(
"Hiiiiii My Frieeeeend!!!!!!!".replaceAll("(.)(?=\\1\\1)", "")
); // prints "Hii My Frieend!!"
I have a website I want to regexp on, say http://www.ru.wikipedia.org/wiki/perl . The site is in Russian and I want to pull out all the Russian words. Matching with \w+ doesn't work and matching with \p{L}+ retrieves everything.
How do I do it?
All those answers are overcomplicated. Use this
$text =~/\p{cyrillic}/
bam.
perl -MLWP::Simple -e 'getprint "http://ru.wikipedia.org/wiki/Perl"'
403 Forbidden <URL:http://ru.wikipedia.org/wiki/Perl>
Well, that doesn't help!
Downloading a copy first, this seems to work:
use Encode;
local $/ = undef;
my $text = decode_utf8(<>);
my #words = ($text =~ /([\x{0400}-\x{04ff}]+)/gs);
foreach my $word (#words) {
print encode_utf8($word) . "\n";
}
Okay, then try this:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $response = $ua->get("http://ru.wikipedia.org/wiki/Perl");
die $response->status_line unless $response->is_success;
my $content = $response->decoded_content;
my #russian = $content =~ /\s([\x{0400}-\x{052F}]+)\s/g;
print map { "$_\n" } #russian;
I believe that the Cyrillic character set starts at 0x0400 and the Cyrillic supplement character set ends at 0x052F, so this should get many of the words.
Just leave this here.
Match a specific Russian word
use utf8;
...
utf8::decode($text);
$text =~ /привет/;