How do I match one letter or many in a PHP preg_split style regex

php, regex

Solution

In your case, it's better to use preg_match with its additional parameter and parenthesis:

preg_match("#((?:<|&lt;)%)([\s]*(?:[^ø]*)[\s]*?)(%(?:>|&gt;))#i",$markup, $out);
print_r($out);

Array
(
    [0] => <% your stuff %>
    [1] => <%
    [2] => your stuff
    [3] => %>
)

By the way, check this online tool to debug PHP regexp, it's so useful !

http://regex.larsolavtorvik.com/

EDIT : I hacked the regexp a bit so it's faster. Tested it, it works :-)

Now let's explain all that stuff :

- preg_match will store everything he captures in the var passed as third param (here $out)

- if preg_match matches something, it will be store in $out[0]

- anything that is inside () but not (?:) in the pattern will be stored in $out

The patten in details :

#((?:<|&lt;)%)([\s]*(?:[^ø]*)[\s]*?)(%(?:>|&gt;))#i can be viewed as ((?:<|&lt;)%) + ([\s]*(?:[^ø]*)[\s]*?) + (%(?:>|&gt;)).

((?:<|&lt;)%) is capturing < or &lt; then %
(%(?:>|&gt;)) is capturing % then < or &gt; 
([\s]*(?:[^ø]*)[\s]*?) means 0 or more spaces, then 0 or more times anything that is not the ø symbol, the 0 or more spaces.

Why do we use [^ø] instead of . ? It's because . is very time consuming, the regexp engine will check among all the existing characters. [^ø] just check if the char is not ø. Nobody uses ø, it's an international money symbol, but if you care, you can replace it by chr(7) wich is the shell bell char that's obviously will never be typed in a web page.

EDIT2 : I just read your edit about capturing all the matches. In that case, you´ll use preg_match_all the same way.

Problem

I'm having an issue with my regex. I want to capture <% some stuff %> and i need what's inside the <% and the %> This regex works quite well for that. ``` $matches = preg_split("/<%[\s]*(.*?)[\s]*%>/i",$markup,-1,(PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)); ``` I also want to catch `&amp;% some stuff %&amp;gt;` so I need to capture `<% or &amp;lt;% and %> or %&amp;gt;` respectively. If I put in a second set of parens, it makes preg_split function differently (because as you can see from the flag, I'm trying to capture what's inside the parens. Preferably, it would only match `&amp;lt; to &amp;gt; and < to >` as well, but that's not completely necessary EDIT: The SUBJECT may contain multiple matches, and I need all of them

Original source