Can you make a small example that explains what you are trying to do? Get https://www.linqpad.net/ and build a
small test to experiment with what you are trying to do.
What you have posted is very unclear. Are these great blocks of characters samples of code? or do they represent a sample of the string you are trying to analyse?
Make a small example that anyone can run. Tell us what you would like the result of the
small example to be (and why).
You really have to provide a clear description about the problem without some redundant information from the text editor. Anyway, I tried to remove those useless text from your question. The information is as below:
String fileInformation =
"Purchase Order PO No : 5293274630
Date : 2020.03.16
Supplier Name & Address Buyer Name & Address L10IU1 : ECONOCOM INTERNATIONAL ITALIA S.P.A Samsung Electronics Italia S.p.A Via Marcello Nizzoli, 8 Via Mike Bongiorno, 9 MILANO 20124
Sales Person : 2061971 ESPRI
Purchse Group : QSW
Payment Terms : S160
Purchaser : AAAAA
Currency : EUR
Plant : S425
Destination : CIF / MILANO [SEI]
Plant TO SITE : Use
Code : I
Material Qty Price Per Unit Amount Delivery Material Description Commercial-Code 1
MI-SCBSMT24SM 10 45.62 1 PC 456.20 2020.03.18
MI-SCBSMT24SM,,
MI-SCBSMT24SM 2
MI-SCBSML24SM 10 32.28 1 PC 322.80 2020.03.18
MI-SCBSML24SM,,
MI-SCBSML24SM
Amount : 779.00 Remark 1 / 1";
string line;
//- Holds the entire line
//- Cycle thru the text file 1 line at a time pulling
//- substrings into the variables initialized above
while ((line = sr.ReadLine()) != null)
{
//- Pulling substrings. If there is a problem
//- with the start index and/or the length values
//- an exception is thrown
try
{
if (line.Trim().Contains("PO No"))
{
//Regex regex = new Regex(@"\d{10}");
Match matchOrder = Regex.Match(line.Trim(), @"\d{10}", RegexOptions.IgnoreCase);
if (matchOrder.Success)
{
samsungOrder.OrderNumber = matchOrder.Value;
}
Match matchOrderDate = Regex.Match(line.Trim(), @"\d{4}[.]\d{2}[.]\d{2}", RegexOptions.IgnoreCase);
if (matchOrderDate.Success)
{
samsungOrder.OrderDate = matchOrderDate.Value.Replace(".", "-");
}
//samsungOrder.ExternelReference = Path.GetFileNameWithoutExtension(fileName);
}
One problem is that which lines you want to fetch. As @PaulTheSmith said, it would be appreciated if you could provide a piece of example to illustrate your problem.
If you simply want to know if there is any better way to get the data, I could suggest you directly use string methods to manipulate the input text "fileInformation".
For example,
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Trim().Contains("PO No"))
{
int start = line.IndexOf(":");
string poNum = line.Substring(start).Trim(); // Don't need to use Regex
}
...... // other things you want to do to proceed the string
However, the format of the data is different for the lines after "Material Qty Price Per Unit Amount Delivery Material Description Commercial-Code 1". (I am not sure about the meaning of these words for you). If you are trying to handle these data,
you might need to think about extracting them out and deal with them in a separate thread.
Regex is suitable for those data which has a fixed format, like
email, phone number. Otherwise, the string method is the best way to manipulate strings.
Hope this can help you.
Best regards,
Sean
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
5 Points
12 Posts
String Reader and Extract information
Jul 07, 2020 08:02 AM|Rakib Ahmed|LINK
Hi i am trying to read a long string and i want to extract from that string my required information
<div> <div id="highlighter_479067" class="syntaxhighlighter csharp">String fileInformation =
"Purchase Order PO No : 5293274630 Date : 2020.03.16 Supplier Name & Address Buyer Name & Address L10IU1 : ECONOCOM INTERNATIONAL ITALIA S.P.A Samsung Electronics Italia S.p.A Via Marcello Nizzoli, 8 Via Mike Bongiorno, 9 MILANO 20124 Sales Person : 2061971 ESPRI Purchse Group : QSW Payment Terms : S160 Purchaser : AAAAA Currency : EUR Plant : S425 Destination : CIF / MILANO [SEI]Plant TO SITE : Use Code : I Material Qty Price Per Unit Amount Delivery Material Description Commercial-Code 1 MI-SCBSMT24SM 10 45.62 1 PC 456.20 2020.03.18 MI-SCBSMT24SM,, MI-SCBSMT24SM 2 MI-SCBSML24SM 10 32.28 1 PC 322.80 2020.03.18 MI-SCBSML24SM,, MI-SCBSML24SM Amount : 779.00 Remark 1 / 1"
;
</div> <div class="line number2 index1 alt1">string
line;
//- Holds the entire line
</div> <div class="line number3 index2 alt2"> </div> <div class="line number4 index3 alt1">//- Cycle thru the text file 1 line at a time pulling
</div> <div class="line number5 index4 alt2">//- substrings into the variables initialized above
</div> <div class="line number6 index5 alt1">while
((line = sr.ReadLine()) !=
null
)
</div> <div class="line number7 index6 alt2">{
</div> <div class="line number8 index7 alt1"> </div> <div class="line number9 index8 alt2">//- Pulling substrings. If there is a problem
</div> <div class="line number10 index9 alt1">//- with the start index and/or the length values
</div> <div class="line number11 index10 alt2">//- an exception is thrown
</div> <div class="line number12 index11 alt1">try
</div> <div class="line number13 index12 alt2">{
</div> <div class="line number14 index13 alt1"> </div> <div class="line number15 index14 alt2"> </div> <div class="line number16 index15 alt1">if
(line.Trim().Contains(
"PO No"
))
</div> <div class="line number17 index16 alt2">{
</div> <div class="line number18 index17 alt1"> </div> <div class="line number19 index18 alt2">//Regex regex = new Regex(@"\d{10}");
</div> <div class="line number20 index19 alt1"> </div> <div class="line number21 index20 alt2">Match matchOrder = Regex.Match(line.Trim(),
@"\d{10}"
, RegexOptions.IgnoreCase);
</div> <div class="line number22 index21 alt1"> </div> <div class="line number23 index22 alt2">if
(matchOrder.Success)
</div> <div class="line number24 index23 alt1">{
</div> <div class="line number25 index24 alt2">samsungOrder.OrderNumber = matchOrder.Value;
</div> <div class="line number26 index25 alt1">}
</div> <div class="line number27 index26 alt2"> </div> <div class="line number28 index27 alt1">Match matchOrderDate = Regex.Match(line.Trim(),
@"\d{4}[.]\d{2}[.]\d{2}"
, RegexOptions.IgnoreCase);
</div> <div class="line number29 index28 alt2"> </div> <div class="line number30 index29 alt1">if
(matchOrderDate.Success)
</div> <div class="line number31 index30 alt2">{
</div> <div class="line number32 index31 alt1">samsungOrder.OrderDate = matchOrderDate.Value.Replace(
"."
,
"-"
);
</div> <div class="line number33 index32 alt2">}
</div> <div class="line number34 index33 alt1"> </div> <div class="line number35 index34 alt2">//samsungOrder.ExternelReference = Path.GetFileNameWithoutExtension(fileName);
</div> <div class="line number36 index35 alt1">}
</div> </div>This is a sample of code if u want i can give full code. what is the correct way to extract information from string
Participant
1660 Points
952 Posts
Re: String Reader and Extract information
Jul 08, 2020 02:20 AM|PaulTheSmith|LINK
Can you make a small example that explains what you are trying to do? Get https://www.linqpad.net/ and build a small test to experiment with what you are trying to do.
What you have posted is very unclear. Are these great blocks of characters samples of code? or do they represent a sample of the string you are trying to analyse?
Make a small example that anyone can run. Tell us what you would like the result of the small example to be (and why).
Contributor
2990 Points
880 Posts
Re: String Reader and Extract information
Jul 08, 2020 03:03 AM|Sean Fang|LINK
Hi Rakib Ahmed,
You really have to provide a clear description about the problem without some redundant information from the text editor. Anyway, I tried to remove those useless text from your question. The information is as below:
One problem is that which lines you want to fetch. As @PaulTheSmith said, it would be appreciated if you could provide a piece of example to illustrate your problem.
If you simply want to know if there is any better way to get the data, I could suggest you directly use string methods to manipulate the input text "fileInformation".
For example,
string line; while ((line = sr.ReadLine()) != null) { if (line.Trim().Contains("PO No")) { int start = line.IndexOf(":"); string poNum = line.Substring(start).Trim(); // Don't need to use Regex } ...... // other things you want to do to proceed the string
However, the format of the data is different for the lines after "Material Qty Price Per Unit Amount Delivery Material Description Commercial-Code 1". (I am not sure about the meaning of these words for you). If you are trying to handle these data, you might need to think about extracting them out and deal with them in a separate thread.
Regex is suitable for those data which has a fixed format, like email, phone number. Otherwise, the string method is the best way to manipulate strings.
Hope this can help you.
Best regards,
Sean