Page 1 of 1

Find First occurrence of a string in multi line page

Posted: Tue Jun 12, 2012 2:33 am
by computer_guy
Hi,
I want to thank you so much for making so a great product. It is really a lifesaver.
I am interested in finding only the first occurrence of a string in multi line page. Is there a way to do this? Thank you for your time.

Re: Find First occurrence of a string in multi line page

Posted: Tue Jun 12, 2012 10:25 am
by DigitalVolcano
I think this should do it:

(?<!.*?\bmyphrase\b.*?)(\bmyphrase\b)

this will match the first occurance of 'myphrase' in the file

Note, you need the 'Dot matches newline' option turned on, otherwise it will match the first occurance in each line.

Re: Find First occurrence of a string in multi line page

Posted: Tue Jun 12, 2012 1:49 pm
by computer_guy
Thank you so much for you quick reply.

You saved me a lot of time.

Find 2nd, 3rd,4th occurrence of a string in multi line page

Posted: Tue Jun 12, 2012 2:02 pm
by computer_guy
While we are discussing this topic, I was wondering if there was a way to find a particular occurrence of a string (eg. 2nd, 3rd,4th or last)in a multi line file?

Any help is really appreciated.

Re: Find First occurrence of a string in multi line page

Posted: Tue Jun 12, 2012 8:19 pm
by Fool4UAnyway
I think it would be something like this:

((?<!.*?\bmyphrase\b.*?)(\bmyphrase\b)){3}(?<!.*?\bmyphrase\b.*?)(\bmyphrase\b)

This would match all the text up to and including the next occurrence after the first 3 of them, so the fourth match. Depending on if you want to process (change) this text, or only catch it, you may use the short form

((?<!.*?\bmyphrase\b.*?)(\bmyphrase\b)){4}

to just find this single long match, or

replace

(((?<!.*?\bmyphrase\b.*?)(\bmyphrase\b)){3}(?<!.*?\bmyphrase\b.*?))(\bmyphrase\b)

by either

$1

to remove the fourth match

or

$2

to remove all the text up to the fourth match.

Re: Find First occurrence of a string in multi line page

Posted: Thu Nov 08, 2012 6:58 am
by brad000123
@DigitalVolcano thanks .. it work for me

Re: Find First occurrence of a string in multi line page

Posted: Tue Feb 18, 2014 11:07 am
by corbingravely
I was also searching for a way to find the first occurrence of a string in multi line page for a project I am doing. Thanks to DigitalVolcano. The solution was very easy and helpful to solve the issue. It really helped me out.





Thanks
Lourene
Schools in Oman