Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. How to retain characters preceding a match?

How to retain characters preceding a match?

Scheduled Pinned Locked Moved Regular Expressions
regextutorialcomquestion
3 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12944071
    wrote on last edited by
    #1

    I'm using an application called Panorama X (see provue.com) which has a function, RegExReplace, which, in a block of text, replaces every occurrence of a string defined by a regular expression with a different string. For example, RegExReplace(TextBlock,"^[;]","//") will search a block of text called TextBlock for all lines beginning with a semi-colon and will replace the semi-colon with two slashes. In some lines, the ";" is preceded by a number of spaces but I still want to change it. The only way I've been able to do this is to copy the characters before the ";" and do this: RegExReplace(TextBlock,"^[ ]+[;]", +"//") which is a little clumsy. Is there a better way to do it? I'm very new to RegEx. michael

    P 1 Reply Last reply
    0
    • U User 12944071

      I'm using an application called Panorama X (see provue.com) which has a function, RegExReplace, which, in a block of text, replaces every occurrence of a string defined by a regular expression with a different string. For example, RegExReplace(TextBlock,"^[;]","//") will search a block of text called TextBlock for all lines beginning with a semi-colon and will replace the semi-colon with two slashes. In some lines, the ";" is preceded by a number of spaces but I still want to change it. The only way I've been able to do this is to copy the characters before the ";" and do this: RegExReplace(TextBlock,"^[ ]+[;]", +"//") which is a little clumsy. Is there a better way to do it? I'm very new to RegEx. michael

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      Look in the documentation of your regex engine for "capture groups". You can use a capture group to grab any leading spaces, then reference it in your replacement string. That bit will probably be called "backreference". If you are doing anything serious with regex, I can heartily recommend Expresso[^] Cheers, Peter

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      U 1 Reply Last reply
      0
      • P Peter_in_2780

        Look in the documentation of your regex engine for "capture groups". You can use a capture group to grab any leading spaces, then reference it in your replacement string. That bit will probably be called "backreference". If you are doing anything serious with regex, I can heartily recommend Expresso[^] Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

        U Offline
        U Offline
        User 12944071
        wrote on last edited by
        #3

        I'll check out capture groups - thank you. And thanks for the thought on Expresso but I'm on a Mac. michael

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups