How to solve Preg_match warning? [duplicate] - preg-match

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.

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 do I truncate the last two characters of all files in a directory? [duplicate]

This question already has answers here:
Bash script to remove 'x' amount of characters the end of multiple filenames in a directory?
(3 answers)
Closed 5 years ago.
So pretty simple question. All of the files in my directory are of the form 6bfefb348d746eca288c6d62f6ebec04_0.jpg. I want them to look like 6bfefb348d746eca288c6d62f6ebec04.jpg. Essentially, I want to take off the _0 at the end of every file name. How would I go about doing this with bash?
With Perl's standalone rename command:
rename -n 's/..(\....)$/$1/' *
If everything looks fine, remove -n.
It is possible to use this standalone rename command with a syntax similar to sed's s/regexp/replacement/ command. In regex a . matches one character. \. matches a . and $ matches end of line (here end of filename). ( and ) are special characters in regex to mark a subexpression (here one . and three characters at the end of your filename) which then can be reused with $1. sed uses \1 for first back-reference, rename uses $1.
See: Back-references and Subexpressions with sed

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 fix Preg_match () warning? [duplicate]

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 :.

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