Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex Help

Status
Not open for further replies.

webmigit

Programmer
Aug 3, 2001
2,027
US
I'm a cold fusion programmer, so what am I doing here? No one over at the cf forum has answered my important regex question.. As matter of fact, I learned what I know of regex from php sites and just adapted to CF...

Here's what and how I want to replace (I don't need the replace functions, I just need to know what regex code to use):

Replace:
Code:
[b](*1)[/b][code] with [code]<b>\1</b>

That's easy, done it already but the problem is:

If the post is like...
Code:
[b]test[/b] [b]it[/b]

it comes out:
Code:
test[/b] [b]it

Any ideas?

Oh if you guys would like to the cf replace function so you can optimumly help:

Code:
#REReplaceNoCase(string,&quot;RegEx_Code_Here&quot;,&quot;ReplaceTextHere_CanUse\1\2etc&quot;,&quot;ALL&quot;)#

Tony Did I help?
Vote!
 
If ColdFusion uses standard regexps, try this:
Code:
#REReplaceNoCase(string,&quot;[b]([^[]*)[/b]&quot;,&quot;<b>\1</b>&quot;,&quot;ALL&quot;)#
The [^[]* means that it matches any number of characters that isn't a [. When you use .*, it matches a string as long as possible. //Daniel
 
Thanks but...

Code:
The problem with that being is what do I do about the [i][/i] and [u][/u]?

Is there anyway to say [^]?

Tony Did I help?
Vote!
 
oops.. is there any way to say
Code:
[^[b][/b]]
? Did I help?
Vote!
 
I'm not sure how you would solve this in ColdFusion, as I have never used it. If it can handle Perl compatible regexps, you have to use the s modifier.
The regexp in Perl would look like this:
Code:
s/[b](.*)[/b]/<b>$1</b>/gs;
I think you should somehow be able to convert this to ColdFusion ;-) //Daniel
 
Do you have non-greed matching available?

Your attempt in your original post was matching the initial and final tags in your string because most regular expression engines, unless told otherwise, match as much as possible.

In the case of &quot;
Code:
[b]test[/b] [b]it[/b]
&quot;, the greedy match for your &quot;*&quot; expression is everything after the opening &quot;
Code:
[b]
&quot; through just before the last &quot;
Code:
[/b]
&quot;.

In PHP, you could use perl-compatible regular expressions to force a non-greedy match.

Where the PHP PCRE-regex equivalent of your original expression is &quot;/\[b\](.*)\[\/b\]/&quot; and produces the same results, the non-greedy PCRE-regex &quot;/\[b\](.*?)\[\/b\]/&quot; matches the way you want. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top