Solved

Regex to find location of a recurring string

Posted on 2009-05-07
6
314 Views
Last Modified: 2012-08-14
Greetings,
I have a long text file with some HTML formatting in it.  Within this file I need to find the start and end points of a particular string.  These strings were endnotes that somehow got seen as regular text when saved as PDF and now need to be removed.

The string I'm searching for starts with
<P>US-United States industry only.

and ends with
United States industries are comparable.</P>

The kicker is that the string may or may not have leading or trailing spaces within the <P> tags.  So what I want to do, is find the starting and ending locations of this string and remove the text between them
0
Comment
Question by:andy_ee
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 24325882
0
 

Author Comment

by:andy_ee
ID: 24325940
I am aware of the indexof function.  My problem is that leading and trailing spaces *within* the paragraph tags.
0
 
LVL 13

Accepted Solution

by:
iHadi earned 500 total points
ID: 24326449
Hi

What I understand from your question is that you have a text that looks like this:

<P>  US-United States industry only. text text United States industries are comparable.  </P>

and you want it like this:

<P>US-United States industry only. text text United States industries are comparable.</P>

to do so you can use the replace function of the Regex:

string input = "...";

string pattern = @"(?<start><P>)\s*(?<text>US-United States industry only\..*United States industries are comparable\.)\s*(?<end></P>)";

string replacement = @"${start}${text}${end}";
 

string result = Regex.Replace(input, pattern, replacement);
 

Console.WriteLine(result);

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:andy_ee
ID: 24326554
You are *SO* close!

I want to find and remove a string that starts with:
"<P>US-United States industry only." or "<P> US-United States industry only."

and ends with:
"United States industries are comparable.</P>" or "United States industries are comparable. </P>"

Please note the spaces after the <P> tag and before the </P> tag.
0
 
LVL 13

Expert Comment

by:iHadi
ID: 24326743
The previous code does that exactly
0
 

Author Closing Comment

by:andy_ee
ID: 31578998
Excellent!  Thanks!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now