Author Topic: Do Non-obligatory (optional) subpatterns exist?  (Read 674 times)

0 Members and 1 Guest are viewing this topic.

Offline GoldeneyeTopic starter

  • Enthusiast
  • Posts: 315
    • View Profile
Do Non-obligatory (optional) subpatterns exist?
« on: December 30, 2009, 03:39:51 AM »
The Question
Is it possible to make a subpattern non-obligatory in the same way that you that you can make alpha-numeric characters (ex: "https?" matches both "http" and "https" non-obligatory?

The Reason
The reason I ask is because I'm trying to match a BB-Code tag with an optional-attribute. What I'm trying to match is [img=<URL>( align=<left|center|right>)] where the " align=..." is non-obligatory.

Currently I am using:
Code: [Select]
'/(\[img=(.*?)\])|(\[img=(.*?) align=(left|center|right))\]/i'
This patterns works perfectly, but in search of something (potentially) more efficient... I am trying to utilize optional-subpatterns:
Code: [Select]
Attempt 1:
'/(\[img=(.*?)(?:(\s(align=(left|center|right))))\]/i'

Attempt 2:
'/(\[img=(.*?)(\s(align=(left|center|right)))?\]/i'

So is what I'm attempting to do completely oblivious of me? I did check the PHP-Manual for some help and I came across Conditional-Subpatterns, but I Non-Obligatory Subpatterns seemed to be the way to go.
"When you do things right, people won't be sure you've done anything at all."

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Do Non-obligatory (optional) subpatterns exist?
« Reply #1 on: December 30, 2009, 06:01:14 AM »
Aside from having more brackets than you appear to need you don't seen far off, something like this should suffice.

Code: [Select]
'#\[img=(.*?)(?: align=(left|center|right))?\]#i'
Of course you asked about pattern matching, whereas in this situation the trick will probably be the replacement.
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Do Non-obligatory (optional) subpatterns exist?
« Reply #2 on: December 30, 2009, 06:35:04 AM »
Alternatively you could save yourself the horror and use pecl bbcode extension http://php.net/manual/en/book.bbcode.php or the PEAR extension http://pear.php.net/package/HTML_BBCodeParser
Developer from Belgium, Vlaams-Brabant

Offline GoldeneyeTopic starter

  • Enthusiast
  • Posts: 315
    • View Profile
Re: Do Non-obligatory (optional) subpatterns exist?
« Reply #3 on: December 30, 2009, 06:32:06 PM »
Alternatively you could save yourself the horror and use pecl bbcode extension http://php.net/manual/en/book.bbcode.php or the PEAR extension http://pear.php.net/package/HTML_BBCodeParser

I would take that route instead but unfortunately, the server I'm using doesn't have the BBCode Extension installed. I can't install it either as it isn't my server. =\

Aside from having more brackets than you appear to need you don't seen far off, something like this should suffice.

Code: [Select]
'#\[img=(.*?)(?: align=(left|center|right))?\]#i'
Of course you asked about pattern matching, whereas in this situation the trick will probably be the replacement.

I took the Expression you provided and modified it slightly. I made the first subpattern greedy by removing the "(*?)" and replaced it with a subpattern that only matches URLs:
Code: [Select]
'#\[img=(https?://\S+)(?: align=(left|center|right))?\]#i'Now it will work as a matching-expression.

I tested it:
Code: [Select]
<?php
    $str 
'[img=http://google.com align=left]';
if(preg_match_all('#\[img=(https?://\S+)(?: align=(left|center|right))?\]#i'$str$match))
print_r($match);
else
echo '$str was not matched!';
?>


With that, you should get an array:
Code: [Select]
Array
(
    [0] => Array
        (
            [0] => [img=http://google.com align=left]
        )
 
    [1] => Array
        (
            [0] => http://google.com
        )
    [2] => Array
        (
            [0] => left
        )
 
)

Thank you very much for your replies, cags and ignace! :)
« Last Edit: December 30, 2009, 06:34:39 PM by Goldeneye »
"When you do things right, people won't be sure you've done anything at all."