remove if is not in front one of this character !?:;% - preg-replace

I would like to remove all if is not in front one of this character !?:;% with preg_replace ( I supose ) .
<div> Hello !
Am I 100 % clear ? </div>
It should give me
<div>Hello ! Am I 100 % clear ?</div>
Thanks in advance

Use negative lookahead:
$str = preg_replace('/ (?![!?:;%])/', '', $str);

Related

Convert \x3c/div\x3e to <div> using perl

I have a string which has characters like \x3c/div\x3e , i am trying to convert this to <div> is there any module which helps to solve this issue. I have checked use HTML::Entities but couldn't solve the issue, i am in need of some suggestion.
One method:
my $str = '\x3c/div\x3e';
$str =~ s{\\x([[:xdigit:]]{2})}{chr hex $1}eg;
print $str;
Outputs
<div>

perl get the first matched word in a line

Sorry im new in perl and cannot find a similar answer.
html file
<div class="user_rating">
.
.
<span class="genre">
.
.
.
<span class="genre">
.
.
.
<span class="genre">
.
.
.
<span class="genre">
perl file
$content =~ /<div class="user_rating">(.*)<span class="genre">/gs;
$empty = $1;
this $empty variable contains information from <div class="user_rating"> to the last <span class="genre">.
But i just want the information from <div class="user_rating"> to the first <span class="genre">.
how should i modify my code? i know it is a regular expression problem.
Any help plz...
Modify your regexp, because .* is greedy.
$content =~ /<div class="user_rating">(.*?)(<span class="genre">){1}/gs;

How can I replace a particular character with its upper-case counterpart?

Consider the following string
String = "this is for test. i'm new to perl! Please help. can u help? i hope so."
In the above string after . or ? or ! the next character should be in upper case. how can I do that?
I'm reading from text file line by line and I need to write modified data to another file.
your help will be greatly appreciated.
you could use a regular expression
try this:
my $s = "...";
$s =~ s/([\.\?!]\s*[a-z])/uc($1)/ge; # of course $1 , thanks to plusplus
the g-flag searches for all matches and the e-flag executes uc to convert the letter to uppercase
Explanation:
with [.\?!] you search for your punctuation marks
\s* is for whitespaces between the marks and the first letter of your next word and
[a-z] matches on a single letter (in this case the first one of the next word
the regular expression mentioned above searches with these patterns for every appearance of a punctuation mark followed by (optional) whitespaces and a letter and replaces it with the result of uc (which converts the match to uppercase).
For example:
my $s = "this is for test. i'm new to perl! Please help. can u help? i hope so.";
$s =~ s/([\.\?!]\s*[a-z])/uc(&1)/ge;
print $s;
will find ". i", "! P", ". c" and "? i" and replaces then, so the printed result is:
this is for test. I'm new to perl! Please help. Can u help? I hope so.
You can use the substitution operator s///:
$string =~ s/([.?!]\s*\S)/ uc($1) /ge;
Here's a split solution:
$str = "this is for test. im new to perl! Please help. can u help? i hope so.";
say join "", map ucfirst, split /([?!.]\s*)/, $str;
If all you are doing is printing to a new file, you don't need to join the string back up. E.g.
while ($line = <$input>) {
print $output map ucfirst, split /([?!.]\s*)/, $line;
}
edit - completely misread the question, thought you were just asking to uppercase the is for some reason, apologies for any confusion!
as the answers so far state, you could look at regular expressions, and the substitution operator (s///). No-one has mentioned the \b (word boundary) character though, which may be useful to find the single is - otherwise you are going to have to keep adding punctuation characters that you find to the character class match (the [ ... ]).
e.g.
my $x = "this is for test. i'm new to perl! Please help. can u help? i hope so. ".
\"i want it to work!\". Dave, Bob, Henry viii and i are friends. foo i bar.";
$x =~ s/\bi\b/I/g; # or could use the capture () and uc($1) in eugene's answer
gives:
# this is for test. I'm new to perl! Please help. can u help? I hope so.
# "I want it to work!". Dave, Bob, Henry viii and I are friends. foo I bar.

In Perl (simple program for newbie needed) , after . or ? or ! the next character should be in upper case [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I replace a particular character with its upper-case counterpart?
Consider following string :
String = "this is for test. i'm new to perl! Please help. can u help? i hope so."
In the above string, after . or ? or ! the next character should be in upper case. Also the first letter of the sentence (string) is to be in uppercase. How can I do that?
I'm reading string character by character.
your help will be greatly appreciated.
regards,
Amit
In a basic way, you can try this code
#!/usr/bin/perl
$String = "this is for test. i'm new to perl! Please help. can u help? i hope so.";
$String =~ s/ ((^\w)|(\.\s\w)|(\?\s\w)|(\!\s\w))/\U$1/xg;
print "$String\n";
(^\w) : beginning of the line
(\.\s\w) : After a '.' followed by a space
(\?\s\w) : After a '?' followed by a space
(\!\s\w) : After a '!' followed by a space
Perl has a ucfirst function that does exactly what you want. So all you need to do it to split your string into sections, use ucfirst on each section and then join them together again.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my $string = q[this is for test. i'm new to perl! Please help. can u help? i hope so.];
my #bits = split /([.?!]\s+)/, $string;
$string = join '', map { ucfirst } #bits;
say $string;

How do I print the '%' character with 'printf'?

Simple problem that I can't figure out...
How can I print a '%' character within a printf string? The code below prints it, but gives an 'invalid conversion' error as well.
printf "\t\t".$hour."00 HRS\t=>\t%.2f\t%.2f\t%.1f\%\n", $total, $max15, ($max15/$total*100);
Should output something like:
0000 HRS => 3125.19 898.02 28.7%
You would use %%, not \% (from man printf)
Instead of \% use %% :)
%% for a single %
On hindsight, there was a crude alternative which would have done the same thing.
Print the '%' symbol as a string:
printf "\t\t".$hour."00 HRS\t=>\t%.2f\t%.2f\t%.1f%s\n", $total, $max15, ($max15/$total*100), "%";
Use %% to print a single %
printf "\t\t".$hour."00 HRS\t=>\t%.2f\t%.2f\t%.1f%%\n", $total, $max15, ($max15/$total*100);
This is a bit tricky because the documentation for the template for printf is actually in the documentation for sprintf. You have to catch that line in the middle of the paragraph to know to look there.