Forum Moderators: coopster & phranque

Message Too Old, No Replies

Regex matching multiple occurrences per file and per line

         

localshred

4:52 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



Hi all,

First post, so here goes. I'm writing a script that does intelligent search and replace on a file tree. Essentially, the script gets each file's contents into a buffer string and performs a match with a pre-defined pattern, in this case the pattern is

/^[^\r\n]*(vendor)[^\r\n]*$/im
. The pattern should find any case-insensitive forms of "vendor" and give me the entire line that was matched. I need to see the whole line in order to determine whether or not to replace the occurrence. This pattern performs well for any number of separate line occurences in a given file.

However, if there are multiple occurrences on a given line, the pattern will only match one of those occurrences, and skip the others. I'm trying to determine if I need to have a second pattern to do additional searching on the matched line, or if I can accomplish this purpose with a single pattern that does all of the above.

Just a sample of what I'm hoping to produce. An example of a single line, multi-occurrence match:

Enthusiastically revolutionize web-enabled VENDOR potentialities vendors installed base e-tailers.

I would like to output something similar to this (again, the ouput isn't the issue here, it's whether or not I can match it all with one pattern):

Multiple occurrences in line: ¦¦ Enthusiastically revolutionize web-enabled VENDOR potentialities vendors installed base e-tailers.¦¦ 
Replace? (y,n,q) ¦¦ ...abled VENDOR poten... ¦¦
Replace? (y,n,q) ¦¦ ...ties vendors insta... ¦¦

Please let me know if I am unclear on my objective. Thanks for any help and response to this!

phranque

8:56 pm on Oct 24, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], localshred!

i think you want the g flag on that pattern...

localshred

9:02 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



I'll try that out... thanks!