...

View Full Version : Best way to change [b]...[/b] to <b>...</b>



chrisvmarle
07-18-2002, 11:28 AM
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:


# 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

amy2go
07-18-2002, 05:04 PM
$to_edit =~ s/\[b\](.*?)\[\/b\]/\<b>$1\<\/b>/isg;

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

-Amy

chrisvmarle
07-18-2002, 06:58 PM
What does it (the questionmark) do?

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

Mzzl, Chris

amy2go
07-18-2002, 07:12 PM
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?

amy2go
07-18-2002, 07:21 PM
forgot to escape those bold tags

i.e. your expression (.*) included anything between \[b\] and \[/b\] including other \[b\] 's and \[/b\] 's

amy2go
07-18-2002, 07:22 PM
Oh well, not very good at VB script here, but you get what I'm saying.

-Amy

dmittner
07-18-2002, 07:30 PM
If I may, I have a similar problem, though my goal is a tab more complex. Say, for example, I have the following:

&#091;b]this is a test&#091;/b]

And what I want to do is replace all the 'i's, between &#091;b] and &#091;/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:



$to_edit =~ s/\[b\](.*?)i(.*?)\[\/b\]/\<b>$1$newchar$2\<\/b>/isg;


Why it no worky? :(

amy2go
07-18-2002, 07:50 PM
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

dmittner
07-18-2002, 07:57 PM
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:



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;

amy2go
07-19-2002, 03:22 PM
You're right, my mistake. It was only replacing the first i. Sorry.

Amy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum