Welcome! Log In Create A New Profile

Advanced

[PHP] Write multiple backslashes

Posted by Danny 
Danny
[PHP] Write multiple backslashes
March 08, 2017 11:00AM
Hi,

I have a file in which I need to interchange two regular expressions every hour
depending on what data will be processed. In other words, I will replace the one
regular expression with the other (they are never together in the same file).
This file is a configuration file for attatched hardware (not written by myself
but by the hardware manufacturer).

Below is what it should look like in the file:
IGNORE_TMP=^()$
or
IGNORE_TMP=^(\\*.*\\*)$" (Yes, this one has 2 backslashes)

Now, the first regular expression I can write to the file like this with no
problems:
$first_regexp = "/IGNORE_TMP=\\^\\(\\)\\$/" ;
which will give me:
IGNORE_TMP=^()$.

However, I cannot seem to get it right to write the second regular expression
correctly to get 2 backlashes. I can get 1 with no problem:
$second_regexp = "IGNORE_TMP=^(\\*.*\\*)$" ;

### Here is just the rest of the code for reference ###
$block_file = file_get_contents ( $tmp_file ) ;
$block_regexp = preg_replace ( $old_block, $new_block, $block_file ) ;
file_put_contents ( $tmp_file, $block_regexp ) ;
$remove_blank_lines = exec ( 'sed -i "/^[[:space:]]*$/d" /etc/scripts/motor_control/temperature.conf' ) ;
#######################################################

Any pointers?

Thank you

Danny

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jason Pruim
Re: [PHP] Write multiple backslashes
March 08, 2017 02:30PM
Sent from my iPhone

> On Mar 8, 2017, at 4:48 AM, Danny <[email protected]> wrote:
>
> Hi,
>
> I have a file in which I need to interchange two regular expressions every hour
> depending on what data will be processed. In other words, I will replace the one
> regular expression with the other (they are never together in the same file).
> This file is a configuration file for attatched hardware (not written by myself
> but by the hardware manufacturer).
>
> Below is what it should look like in the file:
> IGNORE_TMP=^()$
> or
> IGNORE_TMP=^(\\*.*\\*)$" (Yes, this one has 2 backslashes)
>
> Now, the first regular expression I can write to the file like this with no
> problems:
> $first_regexp = "/IGNORE_TMP=\\^\\(\\)\\$/" ;
> which will give me:
> IGNORE_TMP=^()$.
>
> However, I cannot seem to get it right to write the second regular expression
> correctly to get 2 backlashes. I can get 1 with no problem:
> $second_regexp = "IGNORE_TMP=^(\\*.*\\*)$" ;

I'm no regard expert but don't you have to escape it for each one? Something like $second_rexexp = "ignore_tmp=^(\\\\*.*\\\\*)$";

I could be way off base though...


> ### Here is just the rest of the code for reference ###
> $block_file = file_get_contents ( $tmp_file ) ;
> $block_regexp = preg_replace ( $old_block, $new_block, $block_file ) ;
> file_put_contents ( $tmp_file, $block_regexp ) ;
> $remove_blank_lines = exec ( 'sed -i "/^[[:space:]]*$/d" /etc/scripts/motor_control/temperature.conf' ) ;
> #######################################################
>
> Any pointers?
>
> Thank you
>
> Danny
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ryan Pallas
Re: [PHP] Write multiple backslashes
March 08, 2017 02:50PM
On Mar 8, 2017 6:27 AM, "Jason Pruim" <[email protected]> wrote:



Sent from my iPhone

> On Mar 8, 2017, at 4:48 AM, Danny <[email protected]> wrote:
>
> Hi,
>
> I have a file in which I need to interchange two regular expressions
every hour
> depending on what data will be processed. In other words, I will replace
the one
> regular expression with the other (they are never together in the same
file).
> This file is a configuration file for attatched hardware (not written by
myself
> but by the hardware manufacturer).
>
> Below is what it should look like in the file:
> IGNORE_TMP=^()$
> or
> IGNORE_TMP=^(\\*.*\\*)$" (Yes, this one has 2 backslashes)
>
> Now, the first regular expression I can write to the file like this with
no
> problems:
> $first_regexp = "/IGNORE_TMP=\\^\\(\\)\\$/" ;
> which will give me:
> IGNORE_TMP=^()$.
>
> However, I cannot seem to get it right to write the second regular
expression
> correctly to get 2 backlashes. I can get 1 with no problem:
> $second_regexp = "IGNORE_TMP=^(\\*.*\\*)$" ;

I'm no regard expert but don't you have to escape it for each one?
Something like $second_rexexp = "ignore_tmp=^(\\\\*.*\\\\*)$";

I could be way off base though...


Yes that's true but only because it is a double quoted string, meaning
backslashes are treated as escape sequences.

You could also just use a single quoted string.

$second_regexp = 'IGNORE_TMP=^(\\*.*\\*)$';



> ### Here is just the rest of the code for reference ###
> $block_file = file_get_contents ( $tmp_file ) ;
> $block_regexp = preg_replace ( $old_block, $new_block, $block_file ) ;
> file_put_contents ( $tmp_file, $block_regexp ) ;
> $remove_blank_lines = exec ( 'sed -i "/^[[:space:]]*$/d"
/etc/scripts/motor_control/temperature.conf' ) ;
> #######################################################
>
> Any pointers?
>
> Thank you
>
> Danny
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Danny
Re: [PHP] Write multiple backslashes
March 08, 2017 02:50PM
As a matter of interest I got it solved using 8 backslashes ... one would think
that 1 backlash will escape 1 more backlash, but it doesn't. Somehow 8
backlashes did the job ... don't ask me how ...

$IGNORE_TMP=^(\\\\\\\\*.*\\\\\\\\*)$

Thank you for everyone's input ...

>
> I'm no regard expert but don't you have to escape it for each one?
> Something like $second_rexexp = "ignore_tmp=^(\\\\*.*\\\\*)$";
>
> I could be way off base though...
>
>
> Yes that's true but only because it is a double quoted string, meaning
> backslashes are treated as escape sequences. 
>
> You could also just use a single quoted string.
>
> $second_regexp = 'IGNORE_TMP=^(\\*.*\\*)$';

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
Re: [PHP] Write multiple backslashes
March 08, 2017 03:10PM
On Wed, Mar 8, 2017 at 8:40 AM, Danny <[email protected]> wrote:

> As a matter of interest I got it solved using 8 backslashes ... one would
> think
> that 1 backlash will escape 1 more backlash, but it doesn't. Somehow 8
> backlashes did the job ... don't ask me how ...
>
> $IGNORE_TMP=^(\\\\\\\\*.*\\\\\\\\*)$
>
> Thank you for everyone's input ...
>
> >
> > I'm no regard expert but don't you have to escape it for each one?
> > Something like $second_rexexp = "ignore_tmp=^(\\\\*.*\\\\*)$";
> >
> > I could be way off base though...
> >
> >
> > Yes that's true but only because it is a double quoted string, meaning
> > backslashes are treated as escape sequences.
> >
> > You could also just use a single quoted string.
> >
> > $second_regexp = 'IGNORE_TMP=^(\\*.*\\*)$';
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
You do need 4 slashes for each slash. The way it works is the \\\\ becomes
\\ (PHP escapes each \ with \ before sending it to regular expression) then
regular expression treats \\ as \.

If you were outputting it without sending it to regular expression then \\
would be sufficent.
Sorry, only registered users may post in this forum.

Click here to login