[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regular expression woes

Posted on 2002-04-20
2
Medium Priority
?
177 Views
Last Modified: 2008-03-06
I want to find every occurance of a string and be able to replace it with a different string depending on a parameter passed.  (Cryptic, I know)

<mytag=some parameter>stuff inside!</mytag> text inbetween <mytag=param again>more stuff even</mytag>

The problem I am having is that my code will give me "stuff inside!</mytag> text inbetween <mytag=param again>more stuff even" instead of each one seperately.

Here is my code:

$regexp="\[mytag\=([a-zA-Z0-9 ]+)\]([[:graph:] ]+)\[\/mytag\]";
while(eregi($regexp,$message,$regs)){
  if(str_replace(" ","",strtolower("Tom Apps com"))==str_replace(" ","",strtolower($regs[1]))){
    $message=eregi_replace($regexp,"\\2",$message);
  }else{
    $message=eregi_replace($regexp,"",$message);
  }
}
echo $message;

Help if you can! :)
0
Comment
Question by:VBTom
1 Comment
 
LVL 6

Accepted Solution

by:
andreif earned 400 total points
ID: 6959072
Problem is that program looks for the longest match.
To make it look for the shortest, add ? to * or +

Your RegExp:

$regexp="\[mytag\=([a-zA-Z0-9 ]+)\]([[:graph:] ]+)\[\/mytag\]";

should read as

$regexp="\[mytag\=([a-zA-Z0-9 ]+)\]([[:graph:] ]+?)\[\/mytag\]";
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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