Results 1 to 3 of 3
  1. #1

    Question Regular Expression (regex) find and replace

    Hello folks,

    I'm currently trying to tackle some simple Regex to find certain text in my string and encapsulate it in tags. Here's an example of the string I'll be finding/replacing and encapsulating:

    Chapter 4: ROUTING AND RECORDING
    Section 1: The I/O Window
    99: In-Out Overview
    100: Resampling
    101: Submixes
    102: More Complex Routing
    103: Complex Routing - Guitars
    104: Extreme MIDI Routing

    1) "Chapter 4: ROUTING AND RECORDING" needs to be encapsulated in <span style="span1"></span>

    2) "Section 1: The I/O Window" needs to be encapsulated in </span style="style2"></span>

    3) "99: In-Out Overview
    100: Resampling
    101: Submixes
    102: More Complex Routing
    103: Complex Routing - Guitars
    104: Extreme MIDI Routing" needs to be encapsulated in <div style="div1"></div>

    Can someone get me started? I've been fighting with regex and preg_replace for the past couple hours, and haven't been able to get anywhere yet.

    Thanks!
    Jon
    Coding my way to oblivion.

  2. #2
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    587
    Perl or PHP?

    I assume you have multiple chapters and sections, in which case I'd read it into an array and write the array back out line by line.
    If the line starts with Chapter, write the open tags, the line, then close tags.
    Same with if the line starts with Section.
    After the section one is closed, write the opening tag for the rest, the remaining lines, then the closing tag.
    Mike
    cPanel/WHM scripts at Premier Website Solutions (all your website needs)
    Support young figure skaters in training. juniorskaters.com

  3. #3
    Hmm, that's an interesting way to do it. And sorry, I'm using PHP, forgot to mention that.
    Coding my way to oblivion.

Posting Permissions

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