473,799 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,$matche s)

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 1169
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
2734
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 can do so it will be able to create the thumbnail again ? site: www.jfl-media.com Thanks J-F her is the script:
4
1401
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 something different with regex? Note: I ran my expression through "The Regex Coach" by Dr. Weitz and it worked properly. --TekWiz
2
1833
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 preg_match I get an unknown modifier
4
13978
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 '<pre>strlen($regExp) = ', strlen($regExp), "\n"; echo preg_match($regExp, 'sudokusweb.com'); echo '</pre>';
5
7555
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, $res_description); if (!$valid_srch) { ...
6
10252
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", $_FILES) I understand the basic preg_match but am confused as to how the string pattern part is working i.e. "/\." . $allowed_file_types . "$/i"
13
5375
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: preg_match( '/^+$/', $TestString ); OK everything seems fine. However, did you know if you pass the following to preg_match: "12345\n" it will return that a match occurred?!? Even though the newline is not a valid character in our regular...
3
1884
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 the hard coded regular expression. but if I set $text to 12499, it will return 1 for a match. $text = str_repeat('*', 12499);
2
2173
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 preg_match. This expression are to be supplied by user, so I don't have control over it. I just want to suppress this error. I don't want this warning on browser. I also tried using try catch block but no luck.
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.