473,692 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex help with large strings

Hi,

I've seen some postings on this but not exactly relating to this
posting. I'm reading in a large mail message as a string. In the
string is an xml attachment that I need to parse out and remove from
the message once processed. I have to do this as a string and not
using any CDO libraries. My problem is that there's normally a large
pdf in the file so when I read the file in it's massive and I don't
knwo if the XML is at the start/middle or end of the string. My regex
is as follows:

Regex rXMLPart = new Regex(
@"(?<Start>.*)( ?<Middle>Conten t-Type:[^.*?]text\/xml.*?finaldist ributeinformati on.*?\<\/distributionLis t\>)(?<End>.*)" ,
RegexOptions.Ig noreCase |
RegexOptions.Si ngleline |
RegexOptions.Ig norePatternWhit espace);

and a sample of the string is:
-----------------------
Message-ID: <00************ *************** *@csfb.csgroup. com>
From: "Test" <te**@test.co m>
To: <>
Subject: This is a test subject
Date: Thu, 2 Sep 2004 16:58:12 +0100
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_ 0005_01C4910E.0 83D9600"
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409

This is a multi-part message in MIME format.

------=_NextPart_000_ 0005_01C4910E.0 83D9600
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_ 0006_01C4910E.0 83D9600"
------=_NextPart_001_ 0006_01C4910E.0 83D9600
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

This is some body text.
-mark.
------=_NextPart_001_ 0006_01C4910E.0 83D9600
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHT ML 6.00.2800.1458" name=3DGENERATO R>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffff ff>
<DIV><FONT face=3DArial size=3D2>This is some body text.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>***</FONT></DIV></BODY></HTML>

------=_NextPart_000_ 0005_01C4910E.0 83D9600
Content-Type: text/xml;
name="DO_NOT_DE LETE_EMAIL_ATTA CHMENT.XML"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="DO_NO T_DELETE_EMAIL_ ATTACHMENT.XML"

<?xml version="1.0" encoding="UTF-8"?>
<distributionLi st>
</distributionLis t>
------=_NextPart_000_ 0005_01C4910E.0 83D9600
Content-Type: application/pdf;
name="Reader.pd f"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="Reade r.pdf"

JVBERi0xLjUNJeL jz9MNCjkxOTUgMC BvYmo8PC9IWzQzM zk2IDM5MzJdL0xp bmVhcml6ZWQgMS9 F
IDEyMjMzNi9MIDE 1NTUzMDcvTiAxNz kvTyA5MTk5L1QgM TM3MTM2Mz4+DWVu ZG9iag0gICAgICA g
IA14cmVmDTkxOTU gMzYNMDAwMDAwMD AxNiAwMDAwMCBuD QowMDAwMDQ3Njc5 IDAwMDAwIG4NCjA w
MDAwNDMzOTYgMDA wMDAgbg0KMDAwMD A0NzkzNSAwMDAwM CBuDQowMDAwMDQ3 OTk5IDAwMDAwIG4 N
CjAwMDAwNDgyNzY gMDAwMDAgbg0KMD AwMDA0ODMyNyAwM DAwMCBuDQowMDAw MDQ4NjMwIDAwMDA w
IG4NCjAwMDAwNTM 0ODAgMDAwMDAgbg 0KMDAwMDA1MzUxN iAwMDAwMCBuDQow MDAwMDUzOTUyIDA w
.......
------------------------

I've cut the string short but that is the jist of it. If I were to run
against this attached string it all works fine but when really large
(with the rest of the pdf in) the match hangs:

Match mXMLPersonalisa tion = rXMLPart.Match( data);

Could anyone suggest a better way that I should do this. I need to get
the first part and the last part and join thus removing the XML part.
I also need to work on the XML to creat the new messages.

i.e.

string sStartPartOfEma ilMessage =
mXMLPersonalisa tion.Groups["Start"].ToString();
string sXMLPartOfMessa ge =
mXMLPersonalisa tion.Groups["Middle"].ToString();;
string sEndPartOfEmail Message =
mXMLPersonalisa tion.Groups["End"].ToString();;

SendXMLEmail(sS tartPartOfEmail Message, sXMLPartOfMessa ge,
sEndPartOfEmail Message);

Any help would be much appreciated.

-mark.
Nov 16 '05 #1
1 3381
"Mark" <ma*********@cs fb.com> wrote in
news:9a******** *************** ***@posting.goo gle.com...
Hi,

I've seen some postings on this but not exactly relating to this
posting. I'm reading in a large mail message as a string. In the
string is an xml attachment that I need to parse out and remove from
the message once processed. I have to do this as a string and not
using any CDO libraries. My problem is that there's normally a large
pdf in the file so when I read the file in it's massive and I don't
knwo if the XML is at the start/middle or end of the string. My regex
is as follows:

Regex rXMLPart = new Regex(
@"(?<Start>.*)( ?<Middle>Conten t-Type:[^.*?]text\/xml.*?finaldist ributeinformati on.*?\<\/distributionLis t\>)(?<End>.*)" ,
RegexOptions.Ig noreCase |
RegexOptions.Si ngleline |
RegexOptions.Ig norePatternWhit espace);


I haven't done any performance tests with that regex, but I'm quite sure it
will take years if it can *not* find a match on a long string: Here are a
few suggestions:

- Add start/end anchors like these:
@"^(?<Start>.*) (?<Middle>Conte nt-Type:[^.*?]text\/xml.*?finaldist ributeinformati on.*?\<\/distributionLis t\>)(?<End>.*)$ "
So the .* expression in the beginning doesn't have to try every starting
point in the string.
- Couldn't you use Regex.Replace on a pattern like this:
@"(?<Middle>Con tent-Type:[^.*?]text\/xml.*?finaldist ributeinformati on.*?\<\/distributionLis t\>)"
The way regex's work, this should be a lot faster. If you need complex
processing on the string that can't be done with capturing paranthesis, you
could use a MatchEvealuator .
- Finally:
@"Content-Type:[^.*?]text\/xml"
Are you sure about this character class? I'd have expected something like
"\s*" instead of "[^.*?]".

Niki
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5724
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning is to improve, but not to prove.
3
4136
by: Beeeeeves | last post by:
Anyone have any ideas on how fast .NET regexes are when operating on large amounts of text? (the input could be, say, 10KB of text, the regex's pattern would be fairly big ( getting on for 1KB perhaps, but non-too complex as 90% of that would be just lots of words strung together) particularly when using the MatchEvaluator delegate. Thanks
17
3967
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher http://forta.com/books/0672325667/
3
13895
by: WLT | last post by:
I need to filter out non-legal characters from potential (user-entered) file names. Certainly this must exist already. Also...I remember there was a large cache of regex strings online somewhere but I can't remember the site name. It wasn't regex.com (though there is a nice fuzzy animal there). Anyone? PS: It would be very nice to find both Unicode and Ascii versions of the file name filter.
17
2783
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions might be a neat way to solve this, but I am new to them. Can anyone give me a hint here? The catch is, it must only find tokens that are not quoted and not commented; examples follow
15
3228
by: Kay Schluehr | last post by:
I have a list of strings ls = and want to create a regular expression sx from it, such that sx.match(s) yields a SRE_Match object when s starts with an s_i for one i in . There might be relations between those strings: s_k.startswith(s_1) -> True or s_k.endswith(s_1) -> True. An extreme case would be ls = . For this reason SRE_Match should provide the longest possible match. Is there a Python module able to create an optimized regex rx...
3
2293
by: ommail | last post by:
Hi I wonder if regular expressions are in general sower than using classes like String and Char when used for validating/parsing text data? I've done some simple test (using IsMatch()) method and the result was that Regex is either as fast or two times slower than method which used methods from classes
3
1915
by: | last post by:
I'm analyzing large strings and finding matches using the Regex class. I want to find the context those matches are found in and to display excerpts of that context, just as a search engine might. In terms of code, what's the easiest way to make that happen? The code below works fine for identifying the matches, but it doesn't try to extract the surrounding context or display it: currPageText = numberOfMatches =...
4
1944
by: Flomo Togba Kwele | last post by:
I am having difficulty writing a Regex constructor. A line has a quote(") at its beginning and its end. I need to strip both characters off. If the line looks like "1", I need the result to be 1. If it were sed, I could just do s/^"// and s/"$//, but I'm confused with the quote escape characters. Also, can I do both replacements at once? I tried:
0
8611
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8549
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8814
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7642
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6468
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5823
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4332
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2989
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.