laravel preg_replace bold string before string including symbols - preg-replace

I have a data to display which have certain to make it bold.
The data such as below:
Y.B. DATO' SRI HASAN BIN
ARIFIN [ ROMPIN ] minta
I want the word before 'minta' change to bold. Just like below
Y.B. DATO' SRI HASAN BIN
ARIFIN [ ROMPIN ] minta
I have use
$arr = explode("minta", $str, 2);
$first = $arr[0];
$str =preg_replace("^".$first."^", '<b>$0</b>', $str);
But its not working. Please help me.

My answer might looks long. As long as it works, im ok.
$a = explode("[", $str, 2);
$b = $a[0];
$str =preg_replace('~'.$b.'~', '<b>$0</b>', $str);
$str = preg_replace('#\[{1}(.*?)\]{1}#', '<b>$0</b>', $str);

Related

Preg_replace in php - multiple replace

I have $string, which contains:
this example
and I have these 3 expressions:
$pattern = array('/aa*/','/ii*/');
$replacement = array('<i>$0</i>','<b>$0</b>');
preg_replace($pattern, $replacement, $string);
where, preg_replace returns:
th<b>i</b>s ex<<b>i</b>>a</<b>i</b>>mple
and I need output like this:
th<b>i</b>s ex<i>a</i>mple
which means, that I want to replace only characters in original string. Is it possible?
This does the trick in my testing
$pattern = array('/([a-z|^|\s])(aa*)/', '/([a-z|^|\s])(ii*)/');
$replacement = array('$1<i>$2</i>','$1<b>$2</b>');

Perl-How to split a line based on different length of characters

I want to split a line based on multiple character lengths and store them in separate variables.
For ex:$myString = "Mickey 24 USA alive
Here first 12 characters are username, next 2 are age, next 23 characters are country and next 7 are status.
So is there a way to save them separately store them using split() or s///?
Thanks,
Unpacking fixed-width fields is most simply and efficiently done using the unpack built-in function.
Like this
use strict;
use warnings;
my $my_string = 'Mickey 24 USA alive';
my ($username, $age, $country, $status) = unpack 'a12 a2 a23 a7', $my_string;
print <<__END_OUTPUT__;
"$username"
"$age"
"$country"
"$status"
__END_OUTPUT__
output
"Mickey "
"24"
" USA"
" alive"
Use a regex to match, or a substr:
my $myString = "Mickey 24 USA alive";
if ($myString =~ /(.{12})(.{2})(.{23})(.*)/) {
$name = $1;
$age = $2;
$country = $3;
$status = $4;
print "<$name><$age><$country><$status>";
} else {
warn "line not long enough";
}
Outputs:
<Mickey ><24>< USA>< alive>
To strip spacing from the variables after the fact, just use another regex:
$value =~ s/^\s+|\s+$//g;
Can even do that in a single line using:
s/^\s+|\s+$//g for ($name, $age, $country, $status);
Tim Toady's been busy on this one. Roll your own with unpack, as described above by previous posters or perhaps use one from the selection of CPAN modules that make this sort of work a snap.
https://metacpan.org/pod/Parse::FixedLength
https://metacpan.org/pod/Text::FixedLength
Also available
- Data::FixedFormat
- AnyData::Format::Fixed
- Text::FixedWidth
If you have fixed width fields use unpack.
Something like:
my ($username, $age, $country, $status) = unpack("A12A2A23A7", $myString);

use perl to extract a substring between two delimiters and store as a new string

I am working on a Perl script, and I want to split a string between two different variables.
This is my string
<p>Hello my server number is 1221.899999 , please select an option</p>
I want to be able to extract the server number, so I want to split the string after <p>Hello my server number is and before the following space, so my end string would print as
1221.899999
Is regex the best solution for this, rather than using split?
I would just use a regex.
my $str = 'Hello my server number is 1221.899999 , please select an option';
my ($num) = $str =~ /Hello my server number is (\d+\.\d+) ,/;
$num will be undefined if the match didn't succeed.
How about:
$str = 'Hello my server number is 1221.899999 , please select an option';
$str =~ s/^.*\b(\d+\.\d+)\b.*$/$1/;
say $str;
or
$str =~ s/^Hello my server number is (\d+\.\d+)\s.*$/$1/;
If the begining of the string is always that.
output:
1221.899999
I would use regex. How about this:
$str = 'Hello my server number is 1221.899999 , please select an option';
print $1 if $str =~ /is (.*) ,/;
As long as you are sure that there is always a space before the comma, the proper answer is something similar to this
my $string = '<p>Hello my server number is 1221.899999 , please select an option</p>';
my ($server) = $string =~ /server number is (\S+)/;
print $server;
output
1221.899999
If the comma could appear immediately after the end of the server number then you would need to modifiy is slightly to this
my ($server) = $string =~ /server number is ([^\s,]+)/;

in Perl, how do i write regex matched string

I want to write $1 on other line for replacement;
my $converting_rules = +{
'(.+?)' => '$1',
};
my $pre = $converting_rule_key;
my $post = $converting_rules->{$converting_rule_key};
#$path_file =~ s/$pre/$post/; // Bad...
$path_file =~ s/$pre/$1/; // Good!
On Bad, $1 is recognized as a string '$1'.
But I wqnt to treat it matched string.
I have no idea what to do...plz help me!
The trouble is that s/$pre/$post/ interpolates the variables $pre and $post, but will not recursively interpolate anything in them that happens to look like a variable. So you want to add an extra eval to the replacement, with the /ee flag:
$path_file =~ s/$pre/$post/ee;
$x = '$1.00';
print qq/$x/;
prints $1.00, so it's no surprise that
$x = '$1.00';
s/(abc)/$x/;
substitutes with $1.00.
What you have there is a template, yet you did nothing to process this template. String::Interpolate can handle such templates.
use String::Interpolate qw( interpolate );
$rep = '$1';
s/$pat/ interpolate($rep) /e;

Better way to extract elements from a line using perl?

I want to extract some elements from each line of a file.
Below is the line:
# 1150 Reading location 09ef38 data = 00b5eda4
I would like to extract the address 09ef38 and the data 00b5eda4 from this line.
The way I use is the simple one like below:
while($line = < INFILE >) {
if ($line =~ /\#\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*=\s*(\S+)/) {
$time = $1;
$address = $4;
$data = $6;
printf(OUTFILE "%s,%s,%s \n",$time,$address,$data);
}
}
I am wondering is there any better idea to do this ? easier and cleaner?
Thanks a lot!
TCGG
Another option is to split the string on whitespace:
my ($time, $addr, $data) = (split / +/, $line)[1, 4, 7];
You could use matching and a list on LHS, something likes this:
echo '# 1150 Reading location 09ef38 data = 00b5eda4' |
perl -ne '
$,="\n";
($time, $addr, $data) = /#\s+(\w+).*?location\s+(\w+).*?data\s*=\s*(\w+)/;
print $time, $addr, $data'
Output:
1150
09ef38
00b5eda4
In python the appropriate regex will be like:
'[0-9]+[a-zA-Z ]*([0-9]+[a-z]+[0-9]+)[a-zA-Z ]*= ([0-9a-zA-Z]+)'
But I don't know exactly how to write it in perl. You can search for it. If you need any explanation of this regexp, I can edit this post with more precise description.
I find it convenient to just split by one or more whitespaces of any kind, using \s+. This way you won't have any problems if the input string has any tab characters in it instead of spaces.
while($line = <INFILE>)
{
my ($time, $addr, $data) = (split /\s+/, $line)[1, 4, 7];
}
When splitting by ANY kind of whitespace it's important to note that it'll also split by the newline at the end, so you'll get an empty element at the end of the return. But in most cases, unless you care about the total amount of elements returned, there's no need to care.