Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Regex question

Posted on 2011-03-24
4
Medium Priority
?
512 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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

877 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