preg_replace()Delimiter must not be alphanumeric or backslash - preg-match

I have this code :
function queryString(){
//matches up to 10 digits in page number
$query_string = eregi_replace("page=[0-9] {0,10}&","",$_SERVER['QUERY_STRING']);
return $query_string;
}
and when im run it returns this error:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

If you were to use preg_replace, you need a starting and an ending delimiter
$query_string = preg_replace("/page=[0-9] {0,10}&/","",$_SERVER['QUERY_STRING']);
^ ^

Related

Match any character, including special characters using the Match function

I have a relatively weird file names,
but the glob function doesn't seem to pick them up using usual wildcards:
fmt.Println(filepath.Match("/home/catch/*.xml", "/home/catch/{foo/x/y}.xml"))
so, I want to match xml files in the catch folders, and they might have special characters in their name, like {path1/path2}.xml
Sadly, the * wildcard won't match that since I assume slashes and maybe curly braces are considered as non-separator characters?
I'm using Linux and with this pattern Match is returning true . So files like that {path1/path2}.xml (although it is not common), it is possible to match them.
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println(filepath.Match("/home/catch/*\\/*", "/home/catch/{path1/path2}.xml"))
}
Output
true <nil>
But on Windows, escaping is disabled. Instead, \\ is treated as path separator, so it will not work.
"/home/catch/*\\/*"
You can read it in the documentation
pattern:
{ term }
term:
'*' matches any sequence of non-Separator characters
'?' matches any single non-Separator character
'[' [ '^' ] { character-range } ']'
character class (must be non-empty)
c matches character c (c != '*', '?', '\\', '[')
'\\' c matches character c
character-range:
c matches character c (c != '\\', '-', ']')
'\\' c matches character c
lo '-' hi matches character c for lo <= c <= hi
Match requires pattern to match all of name, not just a substring. The only possible returned error is ErrBadPattern, when pattern is malformed.
On Windows, escaping is disabled. Instead, '\\' is treated as path separator.
https://pkg.go.dev/path/filepath#Match

Removing/Replacing reserved or invalid url characters in a string

I'm in the process of creating sef urls for my app. I just encountered an error where one of my objects contains the following characters:
##!*
My desired output is the following where anything illegal outside of reserved/unreserved will get replaced by an underscore:
#_!*
I planned on using this regular expression to filter the bad characters:
[^]A-Za-z0-9_.~!*''();:#&=+$,/?#[%-]+
And do the replacement via gsub
'##!*'.gsub!(/[^]_.~!*''();:#&=+$,/?#[%-]+/, '_')
But not getting anything returned at all. What's going on here?
'<##_!*>'.gsub(/[\[\]^A-Za-z0-9.~!*''();:#&=+$,\/?#%+-]/, '_')
#=> "<_____>"
'[', ']' and '/' must be escaped, '-' must be at the beginning or end of the character class and '^' cannot be at the beginning of the character class (the character class being denoted by the outer '[' and ']' characters). There's no point in replacing '' with '' so I've not included that character in the character class.
Do you wish to also replace '<' and '>'? Are you sure letters and digits "reserved characters"?

Add space at end of a constant char in ABAP

I need to declare this constant:
CONSTANTS: const_sep(3) type c value ' - '.
but with this command, ending space is not considered. How can I declare this constant value?
Short version: You don't. When working with fields of TYPE C (which are always fixed-length fields), the system always trims trailing whitespace. Switch to a TYPE STRING if you need to keep the trailing whitespace, and use the correct delimiters for strings:
CONSTANTS co_sep TYPE string VALUE ` - `.
" ^ ^

Unescape an escaped string

I have a string like
rule = ".radio-inline,\n.checkbox-inline {\n display: inline-block;\n}"
The string contains special characters like \n. How can I return the part of the string before {}?
For the above string it should return ".radio-inline,\n.checkbox-inline "
I tried to write it using
Regexp.escape(rule).match(/(.*){/)[1]
The problem is that it returns:
"\\.radio\\-inline,\\n\\.checkbox\\-inline\\ \\"
This should do it:
rule = ".radio-inline,\n.checkbox-inline {\n display: inline-block;\n}"
rule[/.*(?={)/m]
#=> ".radio-inline,\n.checkbox-inline "
.* matches zero or more characters from the beginning of the line. Being greedy by default, it matches as many characters as it can.
(?={), a positive lookahead, requires the character { to immediately follow the matched string, but it is not part of the match.
m stipulates that the match is to be made over multiple lines.
This uses the form of the method String#[] that takes a regex as an argument.
This regex gets you the part befor {}:
[^"{}]+(?={.*})
Try this demo
However it doesn't select you the " at the and and beggining, I hope this will work for you

PregMatch . space and #?

Can someone tell me, what's wrong in this code:
if ((!preg_match("[a-zA-Z0-9 \.\s]", $username)) || (!preg_match("[a-zA-Z0-9 \.\s]", $password)));
exit("result_message=Error: invalid characters");
}
??
Several things are wrong. I assume that the code you are looking for is:
if (preg_match('~[^a-z0-9\h.]~i', $username) || preg_match('~[^a-z0-9\h.]~i', $password))
exit('result_message=Error: invalid characters');
What is wrong in your code?
the pattern [a-zA-Z0-9 \.\s] is false for multiple reasons:
a regex pattern in PHP must by enclosed by delimiters, the most used is /, but as you can see, I have choosen ~. Example: /[a-zA-Z \.\s]/
the character class is strange because it contains a space and the character class \s that contains the space too. IMO, to check a username or a password, you only need the space and why not the tab, but not the carriage return or the line feed character! You can remove \s and let the space, or you can use the \h character class that matches all horizontal white spaces. /[a-zA-Z\h\.]/ (if you don't want to allow tabs, replace the \h by a space)
the dot has no special meaning inside a character class and doesn't need to be escaped: /[a-zA-Z\h.]/
you are trying to verify a whole string, but your pattern matches a single character! In other words, the pattern checks only if the string contains at least an alnum, a space or a dot. If you want to check all the string you must use a quantifier + and anchors for the start ^ and the end $ of the string. Example ∕^[a-zA-Z0-9\h.]+$/
in fine, you can shorten the character class by using the case-insensitive modifier i: /^[a-z0-9\h.]+$/i
But there is a faster way, instead of negate with ! your preg_match assertion and test if all characters are in the character range you want, you can only test if there is one character you don't want in the string. To do this you only need to negate the character class by inserting a ^ at the first place:
preg_match('/[^a-z0-9\h.]/i', ...
(Note that the ^ has a different meaning inside and outside a character class. If ^ isn't at the begining of a character class, it is a simple literal character.)

Resources