473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with preg_match expression

Hello,

I'm trying to write an expression that will match lines like this:

This is [a] a line[/a] with text
This [w] is also a[/w] a line

but not lines like:

This [a] shall not[/w] match

My expression looks like this:

preg_match("/(.*)\[(.)\](.+)\[\/\2\](.*)/",$line,$matches)

But it does not match. I have tried to replace \2 part of the string above
with a . and then it match.

If I'm correct, the \2 should referre to the character matched by the .
inside the []... or?

Thanx in advace.

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/index.php
Aug 12 '05 #1
2 1154
Jørn Dahl-Stamnes wrote:
"/(.*)\[(.)\](.+)\[\/\2\](.*)/"


a) Escape the backreference; e.g.,
/(.*)\[(.)\](.+)\[\/\\2\](.*)/
b) Single-quote the regular expression.

http://www.php.net/manual/en/languag....syntax.double

--
Jock
Aug 12 '05 #2
John Dunlop wrote:
Jørn Dahl-Stamnes wrote:
"/(.*)\[(.)\](.+)\[\/\2\](.*)/"


a) Escape the backreference; e.g.,
/(.*)\[(.)\](.+)\[\/\\2\](.*)/
b) Single-quote the regular expression.


Thanks. I found the answer just before I red your reply here.

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/index.php
Aug 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Moluske | last post by:
The web site was on another server i switch to a new one but this server hav now PHP Safe Mode to ON so i get error when creating thumbnail. I cant put PHP Safe mode to OFF. is ther any thing i...
4
by: TekWiz | last post by:
I'm using regex to validate data, but preg_match gives the Warning "Unknown modifier '\' " when I have '\\' in the regular expression. Is this a problem with preg_match() or do I have to do...
2
by: splodge | last post by:
I have a simple regular expression: {1,5}x{1,5} Which is designed to detect the width and height of an image from a string, as in: foo 100x100 bar However, when I run this with...
4
by: Clodoaldo Pinto | last post by:
preg_match(): Compilation failed: regular expression too large at offset 0 The regular expression is 34,745 bytes long. <?php $regExp = 'huge regexp with 34,745 bytes'; echo...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
6
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i",...
13
by: chadsspameateremail | last post by:
I might have found a problem with how preg_match works though I'm not sure. Lets say you have a regular expression that you want to match a string of numbers. You might write the code like this:...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
2
ashitpro
by: ashitpro | last post by:
Hi everybody.. The error I am getting is "Warning: preg_match() : Compilation failed: missing terminating ] " This happened because I'd supplied syntactically wrong regular expression to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.