RegExp that looks for a string and only replaced part of it...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Hanford

    RegExp that looks for a string and only replaced part of it...

    I'm after a regular expression that looks through a string for a
    certain code, and then replaces part of that code with some markup:

    So look in this string:
    this text contains (T123) a code.

    And become this string
    this text contains (T<a href="?a=123">T 123</a>) a code.

    The main problem I'm having is that my regexp replaces the whole
    match, including the T:

    $pattern='(T[0-9]{3})';
    $replace='<a href="?a=\1">\1 </a>';

    This results in a hyperlink "?a=T123" when I want ?a=123

    Any suggestions?

    Thanks,

    Mark
  • CC Zona

    #2
    Re: RegExp that looks for a string and only replaced part of it...

    In article <8e709fbf.03072 90456.1ca70d15@ posting.google. com>,
    stuff-google@hanfordo nline.co.uk (Mark Hanford) wrote:
    [color=blue]
    > And become this string
    > this text contains (T<a href="?a=123">T 123</a>) a code.[/color]

    (Did you really intend to double the T like that?)
    [color=blue]
    > The main problem I'm having is that my regexp replaces the whole
    > match, including the T:
    >
    > $pattern='(T[0-9]{3})';
    > $replace='<a href="?a=\1">\1 </a>';
    >
    > This results in a hyperlink "?a=T123" when I want ?a=123
    >
    > Any suggestions?[/color]

    $pattern='T([0-9]{3})';
    $replace='T<a href="?a=\1">T\ 1</a>';

    --
    CC

    Comment

    Working...