简体   繁体   中英

Get only the Nth occurence of a string using .net Regex only

Please some help. I'm trying already by more than 2 days... and it is frustrating for me.

If you have any idea would be so helpful:

Having the following text and using Regex in .NET:

   -------------------------------------
   This is a text   : This is my first line of information
                      which continues here
                    : This is my second line of information
                      which continues here
                    : This is my third line of information
                      which continues here
   -------------------------------------

How could I get:
- "This is my first line of information which continues here"
- "This is my second line of information which continues here"
- "This is my third line of information which continues here"

A practical example: Trying to read line by line
Please consider the text dynamic.

The best regex expression that I was able to obtain is:

 (?ms)((?<=(:)).*?(?=(\:)))
  1. How can I read starting from ": This is my second line" or ": This is my third line"
  2. How can I replace the white spaces between "This is my first line of information" and "which continues here".

Thanks a lot. Radu.

This will get you all you need, but unfortunately also a bit toot much

https://regex101.com/r/5lcWku/1

/:[^:]+$/gm

The issue is that you will never be able to match the last line without also matching everything that comes after that because you have no way of knowing if what comes after the last line is still valid or not (or at least you didn't provide one in our original question).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM