Solved

PHP Regex question

Posted on 2011-03-24
4
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

623 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