How to fix Preg_match () warning? [duplicate] - preg-match

This question already has answers here:
Warning: preg_match() [function.preg-match]: Unknown modifier '/' [duplicate]
(2 answers)
Closed 8 years ago.
I am using preg_match function to my program like this
(preg_match('/^(f|ht)tps?\://', $this->sourceFilename))
But it shows a warning like this
Warning: preg_match() [function.preg-match]: Unknown modifier '/'
How can it modify? Please help me!!!

The last slash should be escaped

You need to escape the / inside the regexp:
preg_match('/^(f|ht)tps?:\//', $this->sourceFilename)
or use a different delimiter:
preg_match('#^(f|ht)tps?:/#', $this->sourceFilename)
BTW, you don't need to escape :.

Related

Jupyter shell assignment passing variables to !sed [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 4 years ago.
This example works properly to find and replace from a Jupyter cell:
all_labels = ['cat', 'dog']
!sed -i 's/num_classes: 90/num_classes: {len(all_labels)}/g' {FILE_PATH}
However this example with the same syntax produces an error:
record_path = '/path/to/data.record'
!sed -i 's/PATH_TO_BE_CONFIGURED\/mscoco_train\.record/{record_path}/g' {FILE_PATH}
I added two forward slashes to escape the backslash and period so my regex tester would recognize the sentence. The error I get is:
sed: -e expression #1, char 50: unknown option to `s'
Anyone know why I can pass a variable in the first example, but not in the second?
I ended up needing to escape the variable too
import re
record_path = re.escape(record_path)

how to supress warning "gets() is deprecated"? [duplicate]

This question already has answers here:
Disable warning: the `gets' function is dangerous in GCC through header files?
(10 answers)
Closed 8 years ago.
everytime i try to input my string using gets() function, my compiler gives me warning like shown below. how to get rid of this. what am i doing wrong?
test.c:27:2: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
gets(temp);
^
Use fgets instead:
fgets(temp, sizeof(temp), stdin);
gets is deprecated because it's dangerous, it may cause buffer overflow.

why we use ##*/ expression with bash variable [duplicate]

This question already has answers here:
explain the linux regex for getting filename
(2 answers)
Closed 8 years ago.
I am tring to understand the bash script.
I am seeing ##* / expression with bash variable.
i.e ${foo##*/}
Can someone please tell me why we use that expression?
It's called "Parameter expansion". The variable $foo is searched for a substring matching the pattern */ (i.e. anything up to a slash) from the beginning (#), and what remains in the variable is returned. Doubling the #-sign makes the matching greedy, i.e. it tries to find the longest possible match.

How to solve Preg_match warning? [duplicate]

This question already has answers here:
PHP regular expressions: No ending delimiter '^' found in
(2 answers)
Closed 9 years ago.
I am using preg_match function in my program. The code is like this
if (!$this->config_allow_src_above_docroot && !preg_match('^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', realpath($this->config_document_root))), $AbsoluteFilename))
But run the application it shows the warning like this
Warning: preg_match() [function.preg-match]: No ending delimiter '^'
Can you help me please..
You must add pattern delimiters:
preg_match('/^' . preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', realpath($this->config_document_root))) . '/', $AbsoluteFilename)
^ ^
Since you have forgotten to put delimiters in your pattern, the regex engine believes that ^ is the starting delimiter and is surprised to not found the same delimiter at the end of the pattern.

After replacing eregi with preg_match I'm getting: preg_match() [function.preg-match]: Unknown modifier ',' [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm using PHP Timeclock and I've had to mod several pages due to outdated code.
However, I'm stuck on this one:
preg_match Unknown modifier ',' in timeclock/admin/timeedit.php on line 274
preg_match ("/^([0-9]{1,2})-,/,.-,/,.$/i", $post_date, $date_regs))
This page was unchanged from the source. Any Ideas?
Try changing your Perl delimiters from / to something else.
got it with preg_match ("^([0-9]{1,2})[-,/,.]([0-9]{1,2})[-,/,.](([0-9]{2})|([0-9]{4}))$^"

Resources