$string = I am a boy
How to substitute whitespaces between words with underscore ?
You need a regular expression and the substitution operator to do that.
my $string = 'I am a boy';
$string =~ s/\s/_/g;
You can learn more about regex in perlre and perlretut. A nice tool to play around with is Rubular.
Also, your code will not compile. You need to quote your string, and you need to put a semicolon at the end.
$string = 'I am a boy';
$string =~ s/ /_/g;
$string =~ tr( \t)(_); # Double underscore not necessary as per Dave's comment
This is just to show another option in perl. I think Miguel Prz and imbabque showed more smarter ways, personally i follow the way imbabque showed.
my $str = "This is a test string";
$str =~ s/\p{Space}/_/g;
print $str."\n";
and the output is
This_is_a_test_string
Related
So i came across a Perl regex "term" which allows you to remove all punctuation. Here is the code:
$string =~ s/[[:punct:]]//g;.
However this proceeds to remove all special characters. Is there a way that particular regex expression can be modified so that for example, it removes all special characters except hyphens. As i stated on my previous question with Perl, i am new to the language, thus obvious things don't come obvious to me. Thanks for all the help :_
Change your code like below to remove all the punctuations except hyphen,
$string =~ s/(?!-)[[:punct:]]//g;
DEMO
use strict;
use warnings;
my $string = "foo;\"-bar'.,...*(){}[]----";
$string =~ s/(?!-)[[:punct:]]//g;
print "$string\n";
Output:
foo-bar----
You may also use unicode property:
$string =~ s/[^-\PP]+//g;
My string looks like this
important stuff: some text 2: some text 3.
I want to only print "important stuff". So basically I want to print everything up to the first colon. I'm sure this is simple, but my regex foo is not so good.
Edit: Sorry I was doing something stupid and gave you a bad example line. It has been corrected.
Just restrict what you're matching to non-colons, [^:]*. Note, the ^ and : boundaries aren't actually needed, but they help document the intent behind the regex.
my $text = "important stuff: some text 2: some text 3."
if ($text =~ /^([^:]*):/) {
print "$1";
}
Consider just splitting on the colon:
use strict;
use warnings;
my $string = 'important stuff: some text 2: some text 3.';
my $important = ( split /:/, $string )[0];
print $important;
Output:
important stuff
Well, assume its a string
$test = "sass sg22gssg 22222 2222: important important :"
Assume you want all characters between.
Wrong answer: $test =~ /:(.+):/; # thank you for the change from .{1,}
Corrected.
$test =~ /:([^:]*):/;
print $1; #perl memory u can assign to a string ;
$found = $1;
As a cheat sheet of regex in perl. cheat sheet
I did test it.
I have a string foo_bar_not_needed_string_part_123. Now in this string I want to remove not_needed_string_part only when foo_ is followed by bar.
I used the below regex:
my $str = "foo_bar_not_needed_string_part_123";
say $str if $str =~ s/foo_(?=bar)bar_(.*?)_\d+//;
But it removed the whole string and just prints a newline.
So, what I need is to remove only the matched (.*?) part. So, that the output is
foo_bar__123.
There's another way, and it's quite simple:
my $str = "foo_bar_not_needed_string_part_123";
$str =~ s/(?<=foo_bar_)\D+//gi;
print $str;
The trick is to use lookbehind check anchor, and replace all non-digit symbols that follow this anchor (not a symbol). Basically, with this pattern you match only the symbols you need to be removed, hence no need for capturing groups.
As a sidenote, in the original regex (?=bar)bar construct is redundant. The first part (lookahead) will match only if some position is followed by 'bar' - but that's exactly what's checked with non-lookahead part of the pattern.
You can capture the parts you do not want to remove:
my $str = "foo_bar_not_needed_string_part_123";
$str =~ s/(foo_bar_).*?(_\d+)/$1$2/;
print $str;
You can try this:
my $str = "foo_bar_not_needed_string_part_123";
say $str if $str =~ s/(foo_(?=bar)bar_).*?(_\d+)/$1$2/;
Outputs:
foo_bar__123
PS: I am new to perl/regex so I am interested if there exist a way to directly replace the matched part. What I have done is captured everything which is required and than replaced the whole string with it.
What's about to divide string to 3 parts, and delete only middle?
$str =~ s/(foo_(?=bar)bar_)(.*?)(_\d+)/$1$3/;
Try this:
(?<=foo_bar_).*(?=_\d)
In this variant, it includes in result ALL (.*) between foo_bar_ and _"any digit".
In your regex, it includes in result:
foo_
Then it looks for "bar" after "foo_":
(?=bar)
But it DOES NOT included at this step. It is included on the next step:
bar_
And then rest of line is included by (.*?)_\d+.
So, in general: it includes in result all this that you typed, EXCEPT (?=bar), which is just looking for "bar" after expression.
go with
echo "foo_bar_not_needed_string_part_123" | perl -pe 's/(?<=foo_bar_)[^\d]+//'
You can use look-behind/look-ahead in this case
$str =~ s/(?<=foo_bar_).*?(?=_\d+)//;
and the look-behind can be replace with \K (keep) to make it a little tidier
$str =~ s/foo_bar_\K.*?(?=_\d+)//;
Here is a sentence like this:
Happy birthday!! I have a good day. :)
I want to know how to process these sentence using regular expression to the following formate:
Happy birthday! I have a good day.
Here's how to do it in PERL (since you didn't specify a programming language.
my $str = "Happy birthday!! I have a good day. :)";
$str =~ s/([.!?]){2,}/$1/g; #remove multiple punctuation
$str =~ s/[:;()]+//g; #remove emoticon
print $str;
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!!"