Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-18-2002, 11:28 AM   PM User | #1
chrisvmarle
Regular Coder

 
Join Date: Jun 2002
Location: the Netherlands
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
chrisvmarle is an unknown quantity at this point
Question Best way to change [b]...[/b] to <b>...</b>

Hi there, I'm looking for a (= the best) way to replace [ b ]Some text[ / b ] (without spaces) by <b>Some text</b>.

I've tried these two:
Code:
# no. 1
$to_edit =~ s/\[b\](.*)\[\/b\]/\<b>$1\<\/b>/isg;

# no. 2
$to_edit =~ s/\[b\]/\<b>/isg;
$to_edit =~ s/\[\/b\]/\<\/b>/isg;
But the firstone only takes the first [ b ] and the last [ / b ], so [ b ]This is bold[ / b ], this not, [ b ]This again [ / b ] will turn out in <b>This is bold[ / b ], this not, [ b ]This again </b>

And the problem with the second one is it is possible to only include [ b ] and so make the rest of the page Bold. wich I don't want my users to be able to.

I hope you understand my problem and maybe even have a solution

Mzzl, Chris
chrisvmarle is offline   Reply With Quote
Old 07-18-2002, 05:04 PM   PM User | #2
amy2go
New Coder

 
Join Date: Jun 2002
Location: Dallas
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
amy2go is an unknown quantity at this point
$to_edit =~ s/\[b\](.*?)\[\/b\]/\<b>$1\<\/b>/isg;

I just added the ? in the inner set of parentheses(.*?)

-Amy
__________________
Some days you are the bug; some days you are the windshield.
amy2go is offline   Reply With Quote
Old 07-18-2002, 06:58 PM   PM User | #3
chrisvmarle
Regular Coder

 
Join Date: Jun 2002
Location: the Netherlands
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
chrisvmarle is an unknown quantity at this point
What does it (the questionmark) do?

I hope you can explain so I can understand and never forget

Mzzl, Chris
chrisvmarle is offline   Reply With Quote
Old 07-18-2002, 07:12 PM   PM User | #4
amy2go
New Coder

 
Join Date: Jun 2002
Location: Dallas
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
amy2go is an unknown quantity at this point
The * is known as a quantifier (which says any number of occurrences). There are other quantifiers:
+ which says one or more occurrences
? which says zero or one occurrences
{n} which says exactly n occurrences
{n,m} which says from n to m, inclusive, occurrences

The * and + quantifiers are greedy, in that they match the maximum number of occurrences that satisfy the regular expression (i.e. your expression (.*) included anything between and including other 's and 's). To make it non-greedy, you can use the ? after the * or + because the ? says zero or one occurrences. This means that it will match the least number of occurrences that satisfy the regular expression.

Did that clear things up?
__________________
Some days you are the bug; some days you are the windshield.
amy2go is offline   Reply With Quote
Old 07-18-2002, 07:21 PM   PM User | #5
amy2go
New Coder

 
Join Date: Jun 2002
Location: Dallas
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
amy2go is an unknown quantity at this point
forgot to escape those bold tags

i.e. your expression (.*) included anything between \[b\] and \[/b\] including other \[b\] 's and \[/b\] 's
__________________
Some days you are the bug; some days you are the windshield.
amy2go is offline   Reply With Quote
Old 07-18-2002, 07:22 PM   PM User | #6
amy2go
New Coder

 
Join Date: Jun 2002
Location: Dallas
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
amy2go is an unknown quantity at this point
Unhappy amy2go

Oh well, not very good at VB script here, but you get what I'm saying.

-Amy
__________________
Some days you are the bug; some days you are the windshield.
amy2go is offline   Reply With Quote
Old 07-18-2002, 07:30 PM   PM User | #7
dmittner
New to the CF scene

 
Join Date: Jul 2002
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
dmittner is an unknown quantity at this point
If I may, I have a similar problem, though my goal is a tab more complex. Say, for example, I have the following:

[b]this is a test[/b]

And what I want to do is replace all the 'i's, between [b] and [/b] with something else, preserving the other characters around the 'i's.

Here's what I'm currently attempting, using a similar syntax as above:

Code:
$to_edit =~ s/\[b\](.*?)i(.*?)\[\/b\]/\<b>$1$newchar$2\<\/b>/isg;
Why it no worky?

Last edited by dmittner; 07-18-2002 at 07:38 PM..
dmittner is offline   Reply With Quote
Old 07-18-2002, 07:50 PM   PM User | #8
amy2go
New Coder

 
Join Date: Jun 2002
Location: Dallas
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
amy2go is an unknown quantity at this point
dmittner >>
Your code worked fine - (of course, you need to initialize the $newchar variable to something first or it will just delete all the i's) like: $newchar = a; will replace all the i's with a's between the opening and closing bold tags. Or you could simply 'hard-code' it like: $1a$2

Amy
__________________
Some days you are the bug; some days you are the windshield.
amy2go is offline   Reply With Quote
Old 07-18-2002, 07:57 PM   PM User | #9
dmittner
New to the CF scene

 
Join Date: Jul 2002
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
dmittner is an unknown quantity at this point
Hmm.. I tested it again and it's only replacing the first 'i', and not the second. The only way I got it to replace the second one was to do:

Code:
while ( $string =~ /\[b\].*?i.*?\[\/b\]/i ){
  $string =~ s/\[b\](.*?)i(.*?)\[\/b\]/\[b]$1SUB$2\[\/b]/isg;
}
$string =~ s/\[b\]/<b>/isg;
$string =~ s/\[\/b\]/<\/b>/isg;
dmittner is offline   Reply With Quote
Old 07-19-2002, 03:22 PM   PM User | #10
amy2go
New Coder

 
Join Date: Jun 2002
Location: Dallas
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
amy2go is an unknown quantity at this point
You're right, my mistake. It was only replacing the first i. Sorry.

Amy
__________________
Some days you are the bug; some days you are the windshield.
amy2go is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:09 PM.


Advertisement
Log in to turn off these ads.