MLUG: Re: [MLUG] String manipulation in C
Re: [MLUG] String manipulation in C
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Sun, 1 Jul 2007, Jack Smith wrote:

So what I'd like to do is hack everything off of the first string after the end of the first match. If I am looking for "ABC" in the following string:

    XXXXXABCXXXXXABCXX

I'd like to reduce the string to

XXXXXABCXX


If I'm understanding your goal, this seems to be the perl way to do it:

s/^(.*?)ABC.*ABC(.*?)$/$1ABC$2/

If you have a bunch of files, you can do them all at once like so:

perl -pi -e 's/^(.*?)ABC.*ABC(.*?)$/$1ABC$2/' file*.txt

(where "ls file*.txt" would list all the files you wish to change and no others).

One thing that wasn't clear from your description is how to handle multiple matches to ABC -- the perl above deletes everything from the first match to the last match. If there is one match in the line, it does nothing.

The question marks are supposed to turn off greediness of the matching algorithm. The first question mark only helps if "ABC" occurs 3 or more times on the line. The second question mark might not do anything, but I put it in there to be safe -- to be sure that .* is greedy and .*? is not greedy when there are multiple ABCs on the line. But maybe the first .* would be greedy anyway so this will do the same thing:

perl -pi -e 's/^(.*?)ABC.*ABC(.*)$/$1ABC$2/' file*.txt

I feel more confident with the question mark in there that it is doing what I want.

Mike

_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members