Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex question - problems with capitilization 1

Status
Not open for further replies.

FNBIT

IS-IT--Management
Oct 27, 2006
74
0
0
US
Hello,

I am pulling info from one of my websites using regular expressions. I am in the process of teaching myself regex but I am confused about something. In a certain sections I want to detect if it is in caps or not. For example this works:

Code:
preg_match('~<title>(.*?)</title>~i', file_get_contents('[URL unfurl="true"]http://www.awebsite.com'),[/URL] $TheTitle );
echo "The Title is $TheTitle[1]";

But when I try to detect if title is TITLE with an or parameter it does not work:

Code:
preg_match('~<title|TITLE>(.*?)</title|TITLE>~i', file_get_contents('[URL unfurl="true"]http://www.awebsite.com'),[/URL] $TheTitle );
echo "The Title is $TheTitle[1]";

I have tried to use parenthesis but that does not help either. What am I doing wrong?

Thanks
 
Hi

While you still have the [tt]i[/tt] modifier, there will be no big difference in specifying alternative strings.

The alternation means either the substring before the pipe ( | ) either the substring after the pipe. So your regular expression means "the red strong OR the green string OR the blue string" :

[tt]~[red]<title[/red]|[green]TITLE>(.*?)</title[/green]|[blue]TITLE>[/blue]~[/tt]

If you want the alternation to be applied only on some shorter substrings, use grouping to delimit them :

[tt]~<([red]title[/red]|[green]TITLE[/green])>(.*?)</([blue]title[/blue]|[purple]TITLE[/purple])>~[/tt]

Note that the additional grouping will do additional capturing. Unless you use non-capturing grouping. Let that be your homework to read about.

Feherke.
 
Thank you! I am understanding it a little more now. I copied the code from another forum and wasn't sure what the i modifier did. With your explanation I learned what it did plus how to group things in shorter substrings.
 
Download REGEX Coach, test and debug all patterns with this before putting them in a PHP script. Its a great lil program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top