Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

replace multiple matches

Last post 01-01-2006, 2:33 PM by George_V_Reilly. 3 replies.
Sort Posts: Previous Next
  •  12-31-2005, 9:48 PM 14509

    replace multiple matches

    DasBlog has a ContentFilter feature that allows you to replace a regular expression with a match. For example, $g(expression) is rewritten as <a href="http://www.google.com/search=expression">expression</a>.

    My problem is when "expression" contains spaces. For the querystring part, the spaces should be replaced by spaces; but not in the visible text of the anchor tag. In other words:
        $g(regex advice)
    should map to
        <a href="http://www.google.com/search=regex+advice">regex advice</a>

    The current rule looks like
        \$g\((?<expr>[\w\s\d]+)\)
        <a href="http://www.google.com/search?q=${expr}">${expr}</a>
    which will leave spaces in the querystring.

    Any ideas?

    /George V. Reilly, Seattle, Wash.
  •  12-31-2005, 9:58 PM 14510 in reply to 14509

    Re: replace multiple matches

    Writing this as two or more rules is a possibility, but I haven't figured out how to express it like that.
  •  01-01-2006, 11:42 AM 14512 in reply to 14510

    Re: replace multiple matches

    What platform are you on? PHP provides preg_replace(), and .NET's RegularExpressions namespace has a MatchEvaluator class that allows you to programmatically handle the replacement. You want to do two entirely unrelated things to the match: HTML-encode it (just spaces?!?), and enclose it in a URL as in your example. No one expression is going to do that for you. If what's inside the parentheses is the match, then that's what the expression will return; you can't also find all the spaces (of which there could be any number, at any location) and replace them with '+' in a single step. I'd be shocked to learn that PHP didn't provide an HTML-encode function that you could invoke within your replacement function. You could also encode spaces by means of a second regex, from within the above replacement function, but that would be unconscionably cute if the platform already provided a way to do it.
    "Some day, and that day may never come, I will call upon you to do a service for me." — Don Vito Corleone
  •  01-01-2006, 2:33 PM 14513 in reply to 14512

    Re: replace multiple matches

    DasBlog is written in ASP.NET: http://www.dasblog.info/. So yes, there's an HTML encoder available, which is a good idea if the expression contains any punctuation characters. As it stands, it only matches [\w\d\s]+, so only the \s needs escaping.

    I was hoping (though with little expectation of success) that a pure regex solution existed. The regex and the replacement both live in an XML config file. The C# source can be modified, of course, to do something with MatchEvaluators.

    The workaround for now is to write out the Google search URL explicitly.
View as RSS news feed in XML