Solved

PHP Regex question

Posted on 2011-03-24
4
504 Views
Last Modified: 2012-05-11
Hello all,
 I'm having problem with php regex. What I want to do is;

$string = "[test-numberTest12-{anotherValue2}]some text here[/test]";

Open in new window


I want my preg_match to match these values. -numberTest12- and {anotherValue2} should be case-insensitive, can contain numbers or letters anywhere (numbers and letters can be anywhere -beginning, end, middle...-)

What I try is a bit like BBCode but I'm not using them as BBCode, that's why I would like to make a underline here, this is not a BBCode but the logic is more or less the same.

I'm not good with regex but I tried;
preg_match("/\[test-([0-9]|[a-z])-\]/", $string)

Open in new window

Doesn't work. I couldn't even get the first part done so I could jump to second value. I will be glad if anyone could help me out with this problem.
0
Comment
Question by:pixalax
  • 2
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35205935
Try this code
<?php

$string = "[test-numberTest12-{anotherValue2}]some text here[/test]";

$pattern = '#.*\[test([^\{]+)\{(.+)\}\]#';

preg_match( $pattern, $string, $matches );

print_r( $matches );

Open in new window

0
 
LVL 2

Author Comment

by:pixalax
ID: 35205967
Hello bportlock;
 I have no idea what you wrote in $pattern but it works. If I might ask you to explain me a bit would it be too much?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35206057
"I have no idea what you wrote in $pattern but it works"

:-D   That's regexes for you......


Let's look at the pattern '#.*\[test([^\{]+)\{(.+)\}\]#' and we will skip the delimiters so we start with

.*\[test([^\{]+)\{(.+)\}\]

That splits up into the following logical sections

.*   \[test   ([^\{]+)   \{  (.+)  \}   \]

The first bit .* says 'there may be characters at the start

Next we have \[test and since [ is a normal regex character we need to escape it with a \ to indicate that it is DATA and not part of the regex controls. test is just the word 'test'. So this bit looks for data matching [test

Next we have ([^\{]+) which is a capture group (inside brackets) that is looking for any data that is NOT a curly brace and that data must occur at  least once (the plus sign)

Next we look for an opening brace \{

Next we look for at least one character (.+)

Next we look for a closing brace \}

and that's pretty much it
0
 
LVL 2

Author Closing Comment

by:pixalax
ID: 35206114
Wow, thank you very much. This is summing up lot's of things for me.

Before I had to use spaces in order to grab [xxx] now I realize with your help i don't need spaces at all, I can use .*\

(If I was using [xxx]text[/xxx][yyy]text[/yyy] it didn't work, I had to use spaces so my regex could grab them.

Thanks a billion for your fast reply and explanation.
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question