#! /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;
Is there an easy way to add regex modifiers such as 'i' to a quoted regular expression? For example:
$pat = qr/F(o+)B(a+)r/;
$newpat = $pat . 'i'; # This doesn't work
The only way I can think of is to print "$pat\n" and get back (?-xism:F(o+)B(a+)r) and try to remove the 'i' in ?-xism: with a substitution
You cannot put the flag inside the result of qr that you already have, because it’s protected. Instead, use this:
$pat = qr/F(o+)B(a+)r/i;
You can modify an existing regex as if it was a string as long as you recompile it afterwards
my $pat = qr/F(o+)B(a+)r/;
print $pat, "\n";
print 'FOOBAR' =~ $pat ? "match\n" : "mismatch\n";
$pat =~ s/i//;
$pat = qr/(?i)$pat/;
print $pat, "\n";
print 'FOOBAR' =~ $pat ? "match\n" : "mismatch\n";
OUTPUT
(?-xism:F(o+)B(a+)r)
mismatch
(?-xism:(?i)(?-xsm:F(o+)B(a+)r))
match
Looks like the only way is to stringify the RE, replace (-i) with (i-) and re-quote it back:
my $pat = qr/F(o+)B(a+)r/;
my $str = "$pat";
$str =~ s/(?<!\\)(\(\?\w*)-([^i:]*)i([^i:]*):/$1i-$2$3:/g;
$pati = qr/$str/;
UPDATE: perl 5.14 quotes regexps in a different way, so my sample should probably look like
my $pat = qr/F(o+)B(a+)r/;
my $str = "$pat";
$str =~ s/(?<!\\)\(\?\^/(?^i/g;
$pati = qr/$str/;
But I don't have perl 5.14 at hand and can't test it.
UPD2: I also failed to check for escaped opening parenthesis.
I need to replace any characters in the set...
[]*?/\:
with an empty string. Can someone post how this is done?
Depends on the language you are using; but you could use (with sed)
sed -e "s/\[\]\*\?\/\\\://g" filename
For C#
Regex.Replace(input, #"[\[\\\]*?/:]", string.Empty)
Perl: $str =~ s/.//g;
But I think you mean replacement with a Space: $str =~ s/./ /g;
For Javascript
var s = "he[ll]o? \*/ foo:bar";
var replaced = s.replace(/[[\\\]*?/:]/g, "");
?replaced
>>hello foobar
I have a line:
$string = 'Paul,12,"soccer,baseball,hockey",white';
I am try to split this into #array that has 4 values so
print $array[2];
Gives
soccer,baseball,hockey
How do I this? Help!
Just use Text::CSV. As you can see from the source, getting CSV parsing right is quite complicated:
sub _make_regexp_split_column {
my ($esc, $quot, $sep) = #_;
if ( $quot eq '' ) {
return qr/([^\Q$sep\E]*)\Q$sep\E/s;
}
qr/(
\Q$quot\E
[^\Q$quot$esc\E]*(?:\Q$esc\E[\Q$quot$esc\E0][^\Q$quot$esc\E]*)*
\Q$quot\E
| # or
[^\Q$sep\E]*
)
\Q$sep\E
/xs;
}
The standard module Text::ParseWords will do this as well.
my #array = parse_line(q{,}, 0, $string);
In response to how to do it with Text::CSV(_PP). Here is a quick one.
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV_PP;
my $parser = Text::CSV_PP->new();
my $string = "Paul,12,\"soccer,baseball,hockey\",white";
$parser->parse($string);
my #fields = $parser->fields();
print "$_\n" for #fields;
Normally one would install Text::CSV or Text::CSV_PP through the cpan utility.
To work around your not being able to install modules, I suggest you use the 'pure Perl' implementation so that you can 'install' it. The above example would work assuming you copied the text of Text::CSV_PP source into a file named CSV_PP.pm in a folder called Text created in the same directory as your script. You could also put it in some other location and use the use lib 'directory' method as discussed previously. See here and here to see other ways to get around install restriction using CPAN modules.
Use this regex: m/("[^"]+"|[^,]+)(?:,\s*)?/g;
The above regular expression globally matches any word that starts with a comma or a quote and then matches the remaining word/words based on the starting character (comma or quote).
Here is a sample code and the corresponding output.
my $string = "Word1, Word2, \"Commas, inbetween\", Word3, \"Word4Quoted\", \"Again, commas, inbetween\"";
my #arglist = $string =~ m/("[^"]+"|[^,]+)(?:,\s*)?/g;
map { print $_ , "\n"} #arglist;
Here is the output:
Word1
Word2
"Commas, inbetween"
Word3
"Word4Quoted"
"Again, commas, inbetween"
try this
#array=($string =~ /^([^,]*)[,]([^,]*)[,]["]([^"]*)["][,]([^']*)$/);
the array will contains the output which expected by you.
use strict;
use warning;
#use Data::Dumper;
my $string = qq/Paul,12,"soccer,baseball,hockey",white/;
#split string into three parts
my ($st1, $st2, $st3) = split(/,"|",/, $string);
#output: st1:Paul,12 st2:soccer,baseball,hockey st3:white
#split $st1 into two parts
my ($st4, $st5) = split(/,/,$st1);
#push records into array
push (my #test,$st4, $st5,$st2, $st3 ) ;
#print Dumper \#test;
print "$test[2]\n";
output:
soccer,baseball,hockey
#$VAR1 = [
# 'Paul',
# '12',
# 'soccer,baseball,hockey',
# 'white'
# ];
$string = "Paul,12,\"soccer,baseball,hockey\",white";
1 while($string =~ s#"(.?),(.?)"#\"$1aaa$2\"#g);
#array = map {$_ =~ s/aaa/ /g; $_ =~ s/\"//g; $_} split(/,/, $string);
$" = "\n";
print "$array[2]";
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!!"