Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    Dallas
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $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.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What does it (the questionmark) do?

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

    Mzzl, Chris

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    Dallas
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    New Coder
    Join Date
    Jun 2002
    Location
    Dallas
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New Coder
    Join Date
    Jun 2002
    Location
    Dallas
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #7
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    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.

  • #8
    New Coder
    Join Date
    Jun 2002
    Location
    Dallas
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;

  • #10
    New Coder
    Join Date
    Jun 2002
    Location
    Dallas
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •