#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2017
    Posts
    4
    Rep Power
    0

    FlimFlam Replace Man


    Hello All,

    Technical writer here looking for some assistance with my regex. I'm attempting to replace two segments, begin and end, with literal text. The third segment, middle, I wish to leave alone. Middle's length is defined by the end of begin and the beginning of end (heh).

    Here's my regex.
    (?<begin><a href=)(?<middle>[^\n\r<]+)(?<end></a>)

    I intend to replace with this regex:
    <Flim:flam href=${middle} is the bomb</Flim:flam>


    And this is sample code

    <a href="../../dir_a/dir_b/topic.htm">Visible Hypertext</a>
    <a href="../a/Setup.htm">It matters not how long this string is</a>
    <a href="../b/thisisthetopicfilename.htm">Hypertextwithoutspaces</a>

    This is how it *should* look, post replace.

    <Flim:flam href="../../dir_a/dir_b/topic.htm">Visible Hypertext is the bomb</Flim:flam>
    <Flim:flam href="../a/Setup.htm">It matters not how long this string is is the bomb</Flim:flam>
    <Flim:flam href="../b/thisisthetopicfilename.htm">Hypertextwithoutspaces is the bomb</Flim:flam>

    The search part seems to work but the replace currently doesn't. I've tried escaping that dollar sign but that didn't work either.

    Thanks!
    -0liver
  2. #2
  3. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,021
    Rep Power
    9616
    Typically replacements are specified with \1 or $1 and named groups with \g{name}, so try
    Code:
    <Flim:flam href=\g{middle} is the bomb</Flim:flam>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2017
    Posts
    4
    Rep Power
    0
    Hey Requinix. Thanks for responding. And sorry for not tagging my code properly. Your suggestion didn't work but it got me thinking that maybe my regex flavor (.net framework) doesn't care about my fancy names so I tried numbers instead. \2 seemed to do the trick.

    Code:
    <Flim:flam href=\2 is the bomb</Flim:flam>
    I now wish to exclude any match when it finds 'http' in the middle. So what should I place inside the middle brackets?

    Code:
    (?<begin><a href=)(?<middle>[^\n\r]+)(?<end></a>)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2017
    Posts
    4
    Rep Power
    0
    Again, I'm looking to add (in addition to the existing new line \n and carriage return \r) a literal text string to that middle segment such that if it finds "http" anywhere in the middle, it moves on. Just like it currently does if it spots a carriage return.

    Am I even able to insert a text string into the middle bracket? In other words, is that middle segment relegated to looking at single characters only?

    Code:
    (?<begin><a href=)(?<middle>[^\n\r]+)(?<end></a>)
    Would I add http after the plus sign but before the closing parenz?
  8. #5
  9. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,021
    Rep Power
    9616
    Originally Posted by Oliver5udden
    I now wish to exclude any match when it finds 'http' in the middle. So what should I place inside the middle brackets?
    How about extending the beginning?
    Code:
    (?<begin><a href=['"]?(?!http))
    Note that will also exclude https. The (?!...) is a forward-looking negative assertion meaning "there must not be ... at this location".

    Requiring text into the middle is simple. Requiring text not be in the middle is also possible, but annoying: the regex has to look character by character all the while checking if the undesired text follows after.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2017
    Posts
    4
    Rep Power
    0
    Yes, I wanted it to exclude both http and https.

    I see what you were driving at and, by gum, I think we've got it. I modified your suggestion to play nice with my regex flavor and it became this:

    Code:
    (?<begin><a href="(?!http))(?<middle>[^\n\r]+)(?<end></a>)
    This code successfully catches all my <a href=" and only discounts them as a match if http follows. This is precisely what I want.

    The replace code had to be altered to account for the now missing double quotes after href=.

    Code:
    <Flim:flam href="\2 is the bomb</Flim:flam>
    And viola! This seems to work.

    Requinix, you rock like Zeppelin. Thank you!
    -Oliver5

IMN logo majestic logo threadwatch logo seochat tools logo