Sign in to follow this  
Followers 0
Zomp

help a newbie about a simple regular expression

10 posts in this topic

Consider the following text:

1
2
3

How can I trasform it to:

1
3

?

I've tried with the following code

$s1="1" & @crlf & "2" & @crlf & "3" & @crlf
msgbox(0,"",$s1)

$s2=StringRegExpReplace ( $s1, "(?s)*?2\r\n(?s)*?", "\0\1")
msgbox(0,"",$s2)

but without success.

Where is my mistake?

Many thanks for your help.

Share this post


Link to post
Share on other sites



You should provide a better example text because I could give you an expression that only extracts the first and last character. Are you wanting the entire first line and the entire last line?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You should provide a better example text because I could give you an expression that only extracts the first and last character. Are you wanting the entire first line and the entire last line?

I agree... this could really get blown out of proportion.

a simple expression : ( $s1, "\r\n2\r\n", @CRLF) would do what you are looking for, but there is always more to the story then what most say in their initial post... and I'd be willing to bet this would not suit your specific needs.

Might be easier to just split the string into an array and remove the specific lines you want.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You should provide a better example text because I could give you an expression that only extracts the first and last character. Are you wanting the entire first line and the entire last line?

Yes, you are right. I apologize.

Suppose I have a text file of several lines and I know that on a line (whose position I do not know) it is written "2" followed by CRLF. Then I want to obtain the same text file but without that line.

I hope to has been enough clear. Thanks for your patience.

Edited by Zomp

Share this post


Link to post
Share on other sites

Yes, you are right. I apologize.

Suppose I have a text file of several lines and I know that on a line (whose position I do not know) it is written "2" followed by CRLF. Then I want to obtain the same text file but without that line.

I hope to has been enough clear. Thanks for your patience.

Hm.. a regular expression for that? Why not:

_FileReadToArray()

a for loop with FileWriteLine(). Write everything EXCEPT the unwanted line

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Hm.. a regular expression for that? Why not:

_FileReadToArray()

a for loop with FileWriteLine(). Write everything EXCEPT the unwanted line

Cheers

Kurt

From my newbie point of view, it seems that your method is much slower than using a single regular expression.

Am I wrong?

Edited by Zomp

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

From my newbie point of view, it seems that your method is much slower than using a single regular expression.

Am I wrong?

it depends on your regular expression and the size of the file. However, as reading and writing the file

takes ~95% of the time anyway, I don't see a big difference (real life not academic).

Clear and easy to understand code is worth much more than a few milliseconds run time.

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

it depends on your regular expression and the size of the file. However, as reading and writing the file

takes ~95% of the time anyway, I don't see a big difference (real life not academic).

Clear and easy to understand code is worth much more than a few milliseconds run time.

Cheers

Kurt

So, if I have well understood, there is a negligible difference between a single filewrite instruction and a loop of filewriteline instructions, the global length of the string being equal. Happy to have learned the lesson.

Edited by Zomp

Share this post


Link to post
Share on other sites

So, if I have well understood, there is a negligible difference between a single filewrite instruction and a loop of filewriteline instructions, the global length of the string being equal. Happy to have learned the lesson.

well, that's not the full truth as well. It heavily depends on the other activities of your system. If you read/write a lot of data while you run the script, it will certainly have a larger impact than if the system is idle (reason: disk head moves).


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0