Re: using regular expressions with find and replace in VC++

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



<jagadeeshbp@xxxxxxxxx> wrote in message
news:1131449289.219062.54700@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
 Thank you for that exhaustive reply. It worked too well. But I need a
bit more help.

1. What if the target is like:
     function(arg1, arg2+arg3, arg4)
and to get
     function(arg1, arg4, arg2+arg3)

The original code seems to do exactly what you ask for.

2. if I need to insert a "new line" between say \1 and \2, what can I
do? I tried putting \1\n\2 in the replace with string. It put
something like \1n\2. How can I do this?

Please do help.
TIA.
--
jag

I have been playing around with this since my first post. There are a number of problems with the original (I don't use this stuff normally, so I am learning by experimentation), one of them being that it doesn't allow for an argument list that spans more than one line. A really robust solution would seem (at best) rather complicated to define and the following falls short of that. It nevertheless improves on my earlier attempt.


A simple approach is to allow for any character in the function argument except , and \). This will allow them to act as a delimiters. [^\),] denotes any character except \) and , and \n. We want to allow \n, so we add that in, making our expression for an argument

[^\),]|\n

We want any number of these, so we use

([^\),]|\n)*

These must be mapped to \1, \2 etc., so we enclose the expression on braces:

{([^\),]|\n)*}

Substituting this for the various function arguments, we arrive at:

func\({([^\),]|\n)*},{([^\),]|\n)*},{([^\),]|\n)*}\)

This seems to work in most cases in terms of matching arguments. (I note that you have gone from using func to using function; naturally this must match too.) It will fail if you make use of , or \) within an argument.

As for inserting new lines, putting \n in the replace string should work (it does for me). Note, however, that the above search string will capture any newline characters in the original function and insert them in the modified one (albeit perhaps in a different position).

--
John Carson


.



Relevant Pages

  • retaining newline characters when writing to file
    ... If I read a string that contains a newline characterinto a variable, then write that variable to a file, how can I retain those newline characters so that the string remains on one line rather than spans multiple lines? ... I'm reading the address field from an HTML form. ... (Or is it better to just use separate text fields for each line of the address?) ...
    (comp.lang.python)
  • Re: Pattern Matching
    ... I'm trying to find out how many newline characters are in a string. ... > Do I need to step through the string a character at a time to check this? ... file with 11 newlines, which I read into a string. ... A language is therefore a horizon, and style a vertical dimension, which together map out for the writer a Nature, since he does not choose either. ...
    (perl.beginners)
  • Re: Selective splits... (treat this "pattern" as a delimiter only if it is followed by this "pattern
    ... Your string must be a single line; no newline characters. ... print a newline for every even numbered element _after_ zero element ... Plus increment counter ...
    (perl.beginners)
  • Reading whole text files
    ... I would appreciate opinions on the following: ... Use fgets(); ugly, if we are not interested in lines ... and have many newline characters to read. ... XSTRgives me BUFLEN in a string literal. ...
    (comp.lang.c)
  • A general solution
    ... Split on an array of delimiters; parse a file in one call. ... Hi Gerry,If the string is using comma to separate name and using quotation ... var element; ... Gerry Hickman wrote: ...
    (microsoft.public.scripting.jscript)