473,491 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regular expression to read multiple groups of text from a text file.

1 New Member
I have an EDI file whose structure is given below. This file has multiple records, each record contains a header(e.g EDI.DD.0000000001.20130809), then contents (i.e multiple paragraphs of text) and then footer (e.g End of Report/No EDI Activity). I have to read that entire file using regular expression using three groups.

I am using following regular expression to read the file.

(?<header>[A-Z]{3}.[A-Z]{2}.[0-9]{10}.[0-9]{8}) | (?<footer> \b(End\sof\sReport|No\sEDI\sActivity)\b) |

(?<content>(?<=\k<header>).*(?=\k<footer>))

That expression reads the "header" and "footer" in respective groups properly but didn't pick the contents between header and footer in "contents" group.

I have changed the font of header and footer in below file to help to understand the format. I am using asp.net 3.5 framework.

Thanks for your help in advance.

//------------------Start of EDI File---------------------//

EDI.DD.0000000001.20130809

ORIGINATOR INFORMATION Company Name: UNITED HEALTHCAR Identification: 9024125001 Originating DFI: 002100002

RECEIVER INFORMATION Receiver Name: HEALTH & WELLNESS DFI Account Number: 0000000000000001 Receiving DFI ID: 434343430 ID Number: Transaction Type: 22 Deposit

ORIGINATOR INFORMATION Company Name: BLUE CHOICE Identification: 9024125001

End of Report

EDI.DD.0006578987.20130809

No EDI Activity

EDI.SV.0000000555.20130809

ORIGINATOR INFORMATION Company Name: Univ of Florida Identification: A426004813 Originating DFI: 004200001

TRANSACTION INFORMATION
Entry Description: vndr pymnt Entry Class Code: CTX Service Class Code: ACH Entries Mixed

REMITTANCE ADVICE ACCOUNTS
RECEIVABLE OPEN ITEM REFERENCE
Seller's Invoice Number: 10016 Pmt Action Code: Amount Paid: $800.00 Amount of Invoice: Amount of Discount:

End of Report

//--------------------End of file------------------------//
Mar 14 '14 #1
1 2029
MichaelvdB
2 New Member
Note: (?<contents>.*?) -- This is a non-greedy match meaning the .* does not try to match the whole string. The RegexOptions.Singleline option treats the input to the Matches function as one long string instead of multiple lines.

Expand|Select|Wrap|Line Numbers
  1. Regex re = new Regex(
  2. "(?<header>[A-Z]{3}.[A-Z]{2}.[0-9]{10}.[0-9]{8})" +
  3. "(?<contents>.*?)" +
  4. "(?<footer>(End of Report|No EDI Activity))",
  5. RegexOptions.Singleline);
  6.  
  7. MatchCollection matches = re.Matches(fileData);
  8.  
  9. foreach(Match m in matches) {
  10.    Console.WriteLine("Header: {0}, Body Length: {1}, Footer: {2}",
  11.       m.Groups["header"].Value,
  12.       m.Groups["contents"].Value.Length,
  13.       m.Groups["footer"].Value);
  14. }
Run results:
Expand|Select|Wrap|Line Numbers
  1. Header: EDI.DD.0000000001.20130809, Body Length: 356, Footer: End of Report
  2. Header: EDI.DD.0006578987.20130809, Body Length: 4, Footer: No EDI Activity
  3. Header: EDI.SV.0000000555.20130809, Body Length: 403, Footer: End of Report
Mar 19 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1397
by: Axel Kowald | last post by:
Hi everybody, I have a 'simple' problem with regular expressions. Maybe someone can help me. import re bla = 'the the' obj = re.search('(.+) \1',bla) obj.group(1) should now be 'the'....
4
3041
by: googlinggoogler | last post by:
Hiya, The title says it all really, but im a newbie to python sort of. I can read in files and write files no probs. But what I want to do is read in a couple of files and output them to one...
32
5889
by: Clunixchit | last post by:
How can i read lines of a file and place each line read in an array? for exemple; array=line1 array=line2 ...
3
1369
by: Arjen | last post by:
Hi, If the users enters some data it must be have 1 ... 100 characters. These characters may be placed on multiple lines. (textbox) I use this: .{1,100} But this only works with one line. ...
1
1365
by: Sreedhar Vankayala | last post by:
Hi, I have a simple textbox in a form page. - If data not available, then user can enter the text "Not available" or "Contact abc person". - If data is available, then user enters the data...
2
1849
by: teo | last post by:
I have a problem (partial). Some days ago I asked for a way to extract a word and few text around it (30 chars on the left and 30 on the right) from a long text. I went good with: ...
0
6978
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...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5451
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,...
1
4881
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...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
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 ...
0
280
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...

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.