Regular Expression-1 (C #)

Source: Internet
Author: User

The reason is that a post asked a question, which is as follows:

Originally Posted People are like this
I want to compile a program, but learning CompSci is a long time ago. I would like to ask you.

There are two TXT files, one is source.txt (with a lot of data) and the other is target.txt (blank)

I want to extract some data from source.txt (slightly modified) and then write it to target.txt.

For example:
Data in sourse.txt:
2oi) 4 @ # ("data: 001% abc"> dsi-23) (* 32 # ("data: dce % xy3" # (* EOIj2308Eld

The data to be extracted is orange.
Data: 001% abc

After all the data is extracted, I want to change % to *, and then add a comma (,) to each piece of data ","

The last target.txt should be like this:

Data: 001 * abc,
Data: dce * xyz

What should I do? JAVA is really forgotten. Tutorial ~~

If you do this for me, you can pay for it.

I used to face similar problems in the past. I always used the program description to solve the problem. Now the problem is raised again, so I have to calm down and think about it. With the 330 Compilation Principle of last semester, and having done finite state automation, we have already been very clear that this kind of text processing should be handed over to Regular Expression (Regular Expression ), it's just that I have never considered it well because the regular expression is obscure. So I am going to take this opportunity to familiarize myself with the Regular Expression. It turns out that the program is so easy to write: using System;
Using System. IO;
Using System. Text;
Using System. Text. RegularExpressions;

Namespace RegExpression
{
/// <Summary>
///
/// </Summary>
Public class DataFilter
{
Public static void Main (string [] args)
{
If (args. Length <2)
{
Console. Error. WriteLine ("Please enter 2 filenames (e.g. In.txt Out.txt )");
Return;
}
String Result;
Using (StreamReader sr = new StreamReader (args [0])
{
Result = Filter (sr. ReadToEnd ());
}
Using (StreamWriter wr = new StreamWriter (args [1])
{
Wr. Write (Result );
}
}
Private static string Filter (string input)
{
StringBuilder result = new StringBuilder ();
Regex r = new Regex ("/"(? <Data> // w + ):(? <Key> // w +) % (? <Value> // w +)/"", RegexOptions. Compiled );
For (Match m = r. Match (input); m. Success; m = m. NextMatch ())
{
Result. Append (m. Result ("$ {data }:$ {key} * $ {value}," + Environment. NewLine ));
}
Return result. ToString ();
}
}
}
The key code to implement this function should be no more than 10 lines, one word is good. Slightly corrected: · using statement · end of line (Environment. NewLine) · use StringBuilder to improve modify mence. Thanks to cumcum for correcting them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.