467,175 Members | 1,325 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,175 developers. It's quick & easy.

alternatives strings syntax in regex (regular expressions)

hi,

i'm using the preg_match_all() function to parse an asp file and find
all include file within this file :

asp include strings can be :
<!--#include virtual="/dir/file.asp"-->
or
<!--#include file="/dir/file.asp"-->

then i write a really simple regex to find them :
<!--#include [virtual|file]="(.*)"-->
and it dont match any include string
i tried
<!--#include (virtual|file)="(.*)"-->
and it works, finding all include strings but returning the value
"file" or "virtual" in the first element of the $matches array
(because of parenthesis), i dont need it.

i dont know if i'm missing something but i saw this "alternative"
syntax on many exemples over the net ( [alternative1|alternative2] )

can someone light my regex quest path ?

thanks a lot
(please excuse my poor english tx)

Germain
Jul 17 '05 #1
  • viewed: 2335
Share:
1 Reply
zOrg wrote:
....
<!--#include [virtual|file]="(.*)"-->
and it dont match any include string
square brackets "[]" specify a character class
Your regexp matches
<!--#include v="anything"-->
and
<!--#include |="someotherthing"-->
and
<!--#include e="nothing"-->
etc, etc, etc :)

i tried
<!--#include (virtual|file)="(.*)"-->
and it works, finding all include strings but returning the value
"file" or "virtual" in the first element of the $matches array
(because of parenthesis), i dont need it.
Right, "()" starts a subpattern that will be grabbed.
The file name is grabbed to the third element of the $matches array
($matches[2]).

To start a subpattern that you don't want to grab use the (?:subpattern)
syntax.
$regexp = '<!--#include (?:virtual|file)="(.*)"-->';

i dont know if i'm missing something but i saw this "alternative"
syntax on many exemples over the net ( [alternative1|alternative2] )

can someone light my regex quest path ?


check the php manual for regexps
http://www.php.net/manual/en/pcre.pattern.syntax.php
http://www.php.net/manual/en/pcre.pattern.modifiers.php

You might want to try "the regex coach"
http://www.weitz.de/regex-coach/

--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Maurice LING | last post: by
4 posts views Thread by Brent | last post: by
8 posts views Thread by vbmark | last post: by
7 posts views Thread by Mike Labosh | last post: by
7 posts views Thread by melanieab | last post: by
5 posts views Thread by Kelth.Raptor | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.