Connecting Tech Pros Worldwide Help | Site Map

alternatives strings syntax in regex (regular expressions)

  #1  
Old July 17th, 2005, 02:37 AM
zOrg
Guest
 
Posts: n/a
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
  #2  
Old July 17th, 2005, 02:37 AM
Pedro Graca
Guest
 
Posts: n/a

re: alternatives strings syntax in regex (regular expressions)


zOrg wrote:
....[color=blue]
><!--#include [virtual|file]="(.*)"-->
> and it dont match any include string[/color]

square brackets "[]" specify a character class
Your regexp matches
<!--#include v="anything"-->
and
<!--#include |="someotherthing"-->
and
<!--#include e="nothing"-->
etc, etc, etc :)

[color=blue]
> 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.[/color]

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)="(.*)"-->';

[color=blue]
> 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 ?[/color]

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 =--
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
RegularExpressionValidator - how to detect strings you dont want? =?Utf-8?B?U2FtdWVs?= answers 1 April 18th, 2007 08:45 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM