I have deprecated "eregi" problem.
if (eregi("data/cheditor4[^<>]*\.(gif|jp[e]?g|png|bmp)", $edit_img, $tmp))
So I changed into this,
if (preg_match("/data/cheditor4[^<>]*\.(gif|jp[e]?g|png|bmp)/i", $edit_img,$tmp))
But I got new warmingmessage,
Warning: preg_match() [function.preg-match]: Unknown modifier 'c'
Please let me know what is wrong.
Thanks in advance.
You have a '/' inside your regex ('data/cheditor') but you are also using '/' as the regex delimiter ('/myregex/flags'): you can either escape the internal '/', or use a different regex delimiter.
E.g. first option:
preg_match('/data\/cheditor4[^<>]*\.(gif|jp[e]?g|png|bmp)/i',...
or (second option, I chose '#' as the delimiter):
preg_match('#data/cheditor4[^<>]*\.(gif|jp[e]?g|png|bmp)#i',...
Also note I changed the " around your regex to ' because otherwise you need to double the backslashes within double-quotes in PHP.
Related
I have a - in my regular expression. When I run it, I get warning: character class has '-' without escape: /[^a-zA-Z0-9-_\\.]/
How can I silence this warning?
At this place [^a-zA-Z0-9-_\\.] there is a possibility of an error. Usage of '-' either means the new range(with mistyping) or just a symbol. Either move single characters in front of ranges or prefix with '\'
verbosity = $VERBOSE
$VERBOSE = nil
/[^a-zA-Z0-9-_\\.]/ #=> /[^a-zA-Z0-9-_\\.]/
$VERBOSE = verbosity
Add a backslash (\) to escape the lonely dash. It appears the offending dash is immediately after [^a-zA-Z0-9. Add \- instead of just the dash character -. The full regex including the minor change is
/[^a-zA-Z0-9\-_\\.]/
This will remove the warning. Escaping it with the backslash clearly instructs the regex engine that you mean to search for that character and not that you forgot to complete a range like [a-z]
I use preg_match to the program like this
if (preg_match('/^[a-z0-9]+\:/{1,2}', $filename))
But it shows an error like this
Warning: preg_match() [function.preg-match]: Unknown modifier '{'
how to change this?
You are missing '/' at the end of the regex and you should escape '/' in the regex itself. This should work i.e. warning should be gone (ignoring if regex you've written is doing what you want):
if (preg_match('/^[a-z0-9]+\:\/{1,2}/', $filename))
I read similar titles but I couldn't make it run..
Now, I have a code like this (originally ereg):
if (preg_match("[^0-9]",$qrcode_data_string)){
if (preg_match("[^0-9A-Z \$\*\%\+\-\.\/\:]",$qrcode_data_string)) {
I also tried using / at the beginning and end of rule but didn't work.
Any replies welcome.
With the preg_* functions you need delimiters around the pattern:
if (preg_match("#[^0-9]#", $qrcode_data_string)) {
# ^ ^
From the documentation:
When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).
I was parsing a file and some lines of the file ended with "\". I wanted to use gsub to find and replace it. I tried '\' and /\/ and neither one correctly matched "\".
I ended up getting around it by using a combination of chop and strip but it left me thinking how would I do this if I ever need to again?
You need to escape the escape sign as well. So this should work:
/\\/
Passing a string to gsub that will then be compiled to a regex:
"abc\def".gsub("\\", "")
=> "abcdef"
Or just providing the regex directly:
"abc\def".gsub(/\\/, "")
=> "abcdef"
In IRB if I pass a string like "/domain/path" to Regexp.escape it just returns it the same. I thought that forward slashes are supposed to be escaped with a backslash? Am I missing something here?
Also, the only reason why you would need to escape / characters is because it is your delimiter for the regexp, if you specify other type of delimiters (or make an instance of the Regexp class) you won't have this issue:
/^hello\/world$/ # escaping '/' just to say: "this is not the end"
%r"^hello/world$" # no need for escaping '/'
Regexp.new('^hello/world$') # no need for escaping '/'
Regexp.escape
Regexp.new(Regexp.escape('/domain/path'))
=> /\/domain\/path/
OR
Regexp.new(Regexp.escape('domain/path'))
=> /domain\/path/