Regular Expression Regular Expression -1 (C #)

Source: Internet
Author: User
Tags expression regular expression
Express| is the cause of a post, asked a question, the post is this:

Originally Posted by people that's it
I want to make a program, but it was a long time ago to learn compsci. I would like to ask you for advice.

There are two txt files, one called Source.txt (with a lot of data), one called Target.txt (blank)

I want to extract some of the data from the Source.txt (modify it slightly) and write it into Target.txt.

As an example:
The data in the Sourse.txt:
2oi) 4@## ("Data:001%abc" >dsi-23) (*32## ("Data:dce%xy3" # (*eoij2308eld

The data you want to extract is orange.
Data:001%abc

After all of this, I'd like to change% to *, and then add a comma to the back of each piece of data.

Finally target.txt should be like this:

DATA:001*ABC,
Data:dce*xyz

What should I do? Really Java forgotten. Ask for advice ~ ~

You can pay me if you do it for me.
I have faced similar problems before, always through the program description of the solution, and now the problem is brought up, so calm down to think about it. With the foundation of the last semester 330 compiler principle, and has done the finite state automatic machine, has already very clear this kind of word processing matter should give regular Expression (regular expression), only oneself always because the regular expression is obscure difficult to understand, therefore did not ponder thoroughly.
So I would like to take this opportunity to regular expression a good understanding. The results found that the program is so good 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))
{
Url 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 is no more than 10 lines is enough, a word, cool.
Slightly modified:
using statement
End of Line (Environment.NewLine)
use StringBuilder to improve performence
These are thanks to Cumcum.



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.