PHP regular expression to match lines starting with a special character
You forgot the multiline modifier (and you should not use the singleline modifier; also the case-insensitive modifier is unnecessary as well as the ungreedy modifier):
preg_match_all("/^#(.*)$/m",$text,$m);
Explanation:
/m
allows the^
and$
to match at the start/end of lines, not just the entire string (which you need here)/s
allows the dot to match newlines (which you don't want here)/i
turns on case-insensitive matching (which you don't need here)/U
turns on ungreedy matching (which doesn't make a difference here because of the anchors)
A PHP code demo:
$text = "1st line\n#test line this \nline #new line\naaaa #aaaa\nbbb#\ncccccccccccc\n#
Related Topics
Differencebetween Site_Url() and Base_Url()
Structure of a Serialized PHP String
How to Set Up Entity (Doctrine) for Database View in Symfony 2
Woocommerce:Add Custom Metabox to Admin Order Page
Post to a Facebook User's Wall with Curl PHP
Php, Get Tomorrows Date from Date
Codeigniter PHP Model Access "Unable to Locate the Model You Have Specified"
Executing a PHP Script with a Cron Job
Set Session in Database in PHP
Why Does PHP Convert a String with the Letter E into a Number
Intermittent PHP Abstract Class Error
PHP See Only 20 Uploading Files at a Time
Xdebug V3 Doesn't Stop Breakpoints in VScode
d";
preg_match_all("/^#(.*)$/m",$text,$m);
print_r($m[0]);
Results:
[0] => #test line this
[1] => #
Related Topics
Differencebetween Site_Url() and Base_Url()
Structure of a Serialized PHP String
How to Set Up Entity (Doctrine) for Database View in Symfony 2
Woocommerce:Add Custom Metabox to Admin Order Page
Post to a Facebook User's Wall with Curl PHP
Php, Get Tomorrows Date from Date
Codeigniter PHP Model Access "Unable to Locate the Model You Have Specified"
Executing a PHP Script with a Cron Job
Set Session in Database in PHP
Why Does PHP Convert a String with the Letter E into a Number
Intermittent PHP Abstract Class Error
PHP See Only 20 Uploading Files at a Time
Xdebug V3 Doesn't Stop Breakpoints in VScode
d
regex - word matching and special characters
Regex:
/(?<!\S)hello(?!\S)/i
PHP:
$text = preg_replace('/(?<!\S)hello(?!\S)/i', "some img url", $text);
You should check if next or previous character is not a non-space character. Word boundaries only apply on boundaries other than [0-9a-zA-Z_]
characters (or any word character in any language)
Regex for not starting with specific word
You can use
^([^:\s]++)(?!:)
and replace with Line:$1
.
See the regex demo. Details:
^
- start of string([^:\s]++)
- Group 1: one or more chars other than:
and whitespace (possessive quantifier prevents backtracking into the[^:\s]
pattern and thus prevents partial matching if a colon is present further in the string)(?!:)
- not immediately followed with a colon.
You may also omit the group and then use ^[^:\s]++(?!:)
and replace with Line:$0
. See this regex demo.
Trying to get regex pattern to match any other than letters and single special character
Use negation in your regex to search for characters you don't want.
// If the string has any characters other than A-z or _ then echo bad string
if (preg_match("/[^a-zA-Z_]/", $string)) {
echo "Bad string";
}
https://regex101.com/r/wKUNjm/2
Regex match with special character
Use the preg_quote
function to escape any special characters that might be in the string:
preg_grep( "/^". preg_quote($name, '/') . "$/i", $values);
From the documentation:
preg_quote()
puts a backslash in front of every character that is part of the regular expression syntax. This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.
Related Topics
Differencebetween Site_Url() and Base_Url()
Structure of a Serialized PHP String
How to Set Up Entity (Doctrine) for Database View in Symfony 2
Woocommerce:Add Custom Metabox to Admin Order Page
Post to a Facebook User's Wall with Curl PHP
Php, Get Tomorrows Date from Date
Codeigniter PHP Model Access "Unable to Locate the Model You Have Specified"
Executing a PHP Script with a Cron Job
Set Session in Database in PHP
Why Does PHP Convert a String with the Letter E into a Number
Intermittent PHP Abstract Class Error
PHP See Only 20 Uploading Files at a Time
Xdebug V3 Doesn't Stop Breakpoints in VScode