Regular Expression Regular Expression - 1 (C #)

xiaoxiao2021-03-06  68

The cause is because of a post, asked a question, the post is like:

Originally posted by

People are like this

I want to make a program, but I learned CompSci for a long time. I would like to ask you.

There are two TXT files, a Source.txt (there are a lot of data), a name called target.txt (blank)

I want to extract some of the data in Source.txt (slightly modified) and write it into target.txt.

for example:

Sourse.txt data:

2OI) 4 @ ## (

"DATA: 001% ABC"> DSI-23) (* 32 ## (

"DATA: DCE% XY3" # (* EOIJ2308ELD

The data that wants to extract is orange.

DATA: 001% ABC

After all of the extracted, I also want to change% to *, then add a comma after each data ","

Last Target.txt should be like this:

Data: 001 * ABC,

Data: DCE * XYZ

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

If you pay for me, you can pay more.

I have also facing similar problems before, I always solve the approach by the procedure, and now the problem is coming, so I calm down to think about it. With the basis of the primary compilation principle, and after the limited state of the automaton, it has been very clear that this kind of text processing should be handed over to Regular Expression (regular expression), but it is difficult to understand because of the regular expression Therefore, there is not good to think.

So I plan to borrow this opportunity to be familiar with the regular expression. The results found that the program was so good:

Using system

;

Using system

.

IO

;

Using system

.

TEXT

;

Using system

.

TEXT

.

RegularExpressions

;

Namespace Regexpression

{

///

/// ////

public

Class

DataFilter

{

public

Static

Void Main

(

String

[]

argg

) {IF

argg

.

Length

<

2

) {

Console

.

Error

.

WriteLine

(

"Please enter 2 filenames (e.g. In.txt out.txt)"

Return;}

String Result

;

Using

(

StreamReader SR

= New

StreamReader

(

argg

[

0

])) {

Result

=

Filter

(

SR

.

ReadToend

());

Using

(

Streamwriter WR

= New

Streamwriter

(

argg

[

1

])) {

WR

.

Write

(

Result

}}

Private

Static

String filter

(

String Input

) {

StringBuilder Result

= New

Stringbuilder ();

Regex R

= New

Regex

(

"/" (? // w ): (? // w )% (? // 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 achieve this function is not more than 10 lines, one word, cool.

Slightly correction:

Using Statement

· End of line (environment.newline)

· Use Stringbuilder to Improve Performen

These thanks to Cumcum gives the correct.

转载请注明原文地址:https://www.9cbs.com/read-110544.html

New Post(0)