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

    Join Date
    Feb 2008
    Posts
    29
    Rep Power
    0

    Regex to search and replace in vi, how to save value...


    Say I want to search for any expression that is being sent to a particular function, and remove the function allowing the expression to stand on its own. That would require matching on the function name and removing it and its brackets. Of course the problem is that the brackets are wrapped around an expression that must stay where it is. Is there anyway to do this with a regexp?

    Example:
    Before:
    $result1 = function($expression);
    $result2 = function($another);
    After:
    $result1 = $expression;
    $result2 = $another;

    Is there anyway to get that closing bracket? Is there a way of saving the portion that you want to keep somehow so that you can put it in the replace portion of the regexp?

    Thanks for any help.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,874
    Rep Power
    6351
    Normally you can put a capture group inside a match expressions so you can use it later. \1 or \\1 is normally the first group of parens. For instance, to do this in PHP:
    PHP Code:
    $file preg_match("/function\(([^\)]+)\)/""\\1"$file); 
    Don't know about vi in particular though.

    -Dan

    Comments on this post

    • s2cuts agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    29
    Rep Power
    0
    Originally Posted by ManiacDan
    Normally you can put a capture group inside a match expressions so you can use it later. \1 or \\1 is normally the first group of parens. For instance, to do this in PHP:
    PHP Code:
    $file preg_match("/function\(([^\)]+)\)/""\\1"$file); 
    Don't know about vi in particular though.

    -Dan
    Yup. Thank you Dan. Grouping and backreferencing is implemented in vim. Here's a link I later found with more detail on the topic:

    http://www.regular-expressions.info/brackets.html

IMN logo majestic logo threadwatch logo seochat tools logo