Solved

Regex to find location of a recurring string

Posted on 2009-05-07
6
315 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Finding (and replacing) text between two strings in Php 7 50
Sum Column in GridView 3 41
PowerShell Regular Expression 2 38
Help with C#, MVC, razor. 6 18
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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…

948 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

18 Experts available now in Live!

Get 1:1 Help Now