Find First occurrence of a string in multi line page

A place to try and solve your RegEx problems.
Post Reply
computer_guy
Posts: 4
Joined: Mon Apr 16, 2012 3:39 pm

Find First occurrence of a string in multi line page

Post 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.
User avatar
DigitalVolcano
Site Admin
Posts: 1729
Joined: Thu Jun 09, 2011 10:04 am

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

Post 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.
computer_guy
Posts: 4
Joined: Mon Apr 16, 2012 3:39 pm

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

Post by computer_guy »

Thank you so much for you quick reply.

You saved me a lot of time.
Last edited by computer_guy on Wed Jun 13, 2012 8:04 pm, edited 1 time in total.
computer_guy
Posts: 4
Joined: Mon Apr 16, 2012 3:39 pm

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

Post 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.
User avatar
Fool4UAnyway

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

Post 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.
brad000123
Posts: 1
Joined: Thu Nov 08, 2012 6:56 am

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

Post by brad000123 »

@DigitalVolcano thanks .. it work for me
corbingravely
Posts: 2
Joined: Fri Nov 22, 2013 5:47 am

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

Post 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
Post Reply