Regex not matching when used with PHP [duplicate] - php

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
Given a fairly simple regex, I'd like to match a text between to delimiters:
___MANUAL_TICKET___
###_CLIENT_START_###
TEST
###_CLIENT_END_###
###_PROBLEM_START_###
TEST2
###_PROBLEM_END_###
###_EMAIL_START_###
xyz#test.com
###_EMAIL_END_###
To get the client I am using this regex:
###_CLIENT_START_###\s(.*?)\s###_CLIENT_END_###
which works as seen HERE.
But when I use it in my PHP Code it does not find any matches:
preg_match('####_CLIENT_START_###\s(.*?)\s###_CLIENT_END_####', $source, $matches);
(tried different regex delimiter such as / and ~, same result)
What am I doing wrong?

Note that the dot (.) by default matches any symbol but the line feed (See the documentation).
Since you have multiple line input, you need to use the PCRE_DOTALL option, which can be enabled just adding symbol s at the very end of the pattern
preg_match('####_CLIENT_START_###\s(.*?)\s###_CLIENT_END_####s', $source, $matches);
^ here

Related

What is the use of "ereg_replace("\n","\\n",$row[$j])" expression? [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 2 years ago.
I found one PHP script for get a database backup, there use this expression to do something, that script show call to undefined function ereg_replace() this error, if i remove this line script is working fine...
how to replace this function $row[$j] = ereg_replace("\n","\\n",$row[$j]); to that script working finely,
Can anyone assist me..
ereg_replace deprecated and remove from newer versions of php (7 and up).
You will have to update your code to use preg_replace
$row[$j] = preg_replace("#\n#","\\n",$row[$j]);
One of the differences between ereg_replace() and preg_replace() is that the pattern must be enclosed by delimiters: delimiter + pattern + delimiter, in this case we are using # so we don't have to escape / that is the usual that is used.
Valid Delimiters:
/, #, ~, +, %, #, ! , <, >
Reference from php.net
hope it helps.

regex to only match a full set of two-part pattern (kinda non-greedy) [duplicate]

This question already has answers here:
Non-greedy string regular expression matching
(2 answers)
Regex lazy quantifier behave greedy
(2 answers)
Closed 3 years ago.
I use preg_match_all to get all matches of a two-part pattern as:
/<(.*?)>[:|\s]+{(.*?)}/
In a string like:
<First>: something <second>: {Second} something <Third>: {Third}
I want to match:
<second>: {Second}
instead of:
<First>: something <second>: {Second}
Working Example
Where did I do wrong?
Use limited repeated set instead of lazy repetition inside the brackets:
<([^>]*)>[:\s]+{(.*?)}
The change is to replace <(.*?)> with <([^>]*)>. The initial version matches the first < then takes lazily any character until it finds :{Second}. If you restrict repetition, regex engine will try to start with <First>, but when it doesn't find :{...} after that, it'll try with the next <
Demo

Regex PHP part of string [duplicate]

This question already has answers here:
Variable-length lookbehind-assertion alternatives for regular expressions
(5 answers)
Closed 4 years ago.
I cant get my regexpression to work in php. It works in javascript (vuejs):
(?<=.+: )(.*)
I have this string:
NL: abcdef
and i would like to get
abcdef
Can someone please tell me what i am doing wrong?
There are many ways to solve this using PHP/PCRE, one is to skip the preceding string using \K
[^:]+: \K(.*)
Regex Demo
If you can add an anchor to the beginning of the string, even better: ^[^:]+: \K(.*)

what does % sign do in preg_match_all? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I am using this library with this line of text
#asdfasdf #日本語 スペース #漢字 #日本語 あ http://url file:///url「おくむら」最高ー!…
And it gives me right values
#asdfasdf #日本語
But in code there is % in regex
'%(\A#(\w|(\p{L}\p{M}?)|-)+\b)|((?<=\s)#(\w|(\p{L}\p{M}?)|-)+\b)|((?<=\[)#.+?(?=\]))%u'
What does this percent sign do?
In iOS it works without percent sign like this.
"(\\A#(\\w|(\\p{L}\\p{M}?)|-)+\\b)|((?<=\\s)#(\\w|(\\p{L}\\p{M}?)|-)+\\b)|((?<=\\[)#.+?(?=\\]))"
In php it gives me error: preg_match_all(): Unknown modifier '|'
What does this percent sign do?
Nothing, it is the delimiter to separate the regex from the additional options. They are required in the preg_* library, see http://php.net/manual/en/regexp.reference.delimiters.php.
Note that if you don't use the % in your regex, the ( ... ) will be used as the delimiter, leading to an error when you need to use them in your regex itself and you don't escape them.
Which is happening here in your case:
(\\A#(\\w|(\\p{L}\\p{M}?)|
^ Here the regex engine thinks you are closing the expression

PHP preg_match (.*) not matching past line breaks [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 2 years ago.
I have this data in a LONGTEXT column (so the line breaks are retained):
Paragraph one
Paragraph two
Paragraph three
Paragraph four
I'm trying to match paragraph 1 through 3. I'm using this code:
preg_match('/Para(.*)three/', $row['file'], $m);
This returns nothing. If I try to work just within the first line of the paragraph, by matching:
preg_match('/Para(.*)one/', $row['file'], $m);
Then the code works and I get the proper string returned. What am I doing wrong here?
Use the s modifier.
preg_match('/Para(.*)three/s', $row['file'], $m);
Pattern Modifiers
Add the multi-line modifier.
Eg:
preg_match('/Para(.*)three/m', $row['file'], $m)
Try setting the regex to dot-all (PCRE_DOTALL), so it includes line breaks (the extra 's' parameter at the end):
preg_match('/Para(.*)three/s', $row['file'], $m);
If you don't like / at the start and and, use T-Regx
$m = Pattern::of('Para(.*)three')->match($row['file'])->first();

Categories