Sign in to follow this  
Followers 0
face

copy starting with any letter instead of word

4 posts in this topic

i have this script, it copies and saves all lines starting with the word "valuable"

how can i make it copy and save all lines starting with any character?

#include <File.au3>
#include <Array.au3>

$file_output = @DesktopDir & "\output.txt"
Local $mypath = @ScriptDir, $lines = ""
Local $aFiles = _FileListToArrayRec($mypath, "*.txt", 1, 1, 0, 2)

For $i = 1 to $aFiles[0]
   $content = FileRead($aFiles[$i])
   $res1 = StringRegExp($content, '(?m)^(valuable\N*)\R?', 3)

   If IsArray($res1) Then
         $lines &= $aFiles[$i] & @CRLF
         $lines &= _ArrayToString($res1, @CRLF) & @CRLF & @CRLF
   EndIf
Next

FileWrite($file_output, $lines)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try altering the regexp:

'(?m)^(' & $sMyLetter & '\N*)\R?'

I can't test it ATM - unfortunately.

A word of warning:  some characters have special significance in regexp patterns. The code should work for alphanumeric input. Regexp is also case sensitive. The following change makes the pattern case insensitive.

'(?i)(?m)^(' & $sMyLetter & '\N*)\R?'

Substitute the regexp into the code you have. Declare the variable $sMyLetter and assign it equal to the character you are looking for. Run the code and see what happens.

Edited by czardas

Share this post


Link to post
Share on other sites

thanks alot

that works for 1 letter at a time. how can i make it work with (-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0) at once

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

'(?im)^([\d-]\N*)\R?'

This will work for lines starting with hyphen or any digit

For easier use you should build a little func with the desired pattern as parameter :)

$aLines = _LinesStartingWith($txt, '[\d-]')

Func _LinesStartingWith($content, $sMyPattern)
   $res = StringRegExp($content, '(?m)^(' & $sMyPattern & '\N*)\R?', 3)
   If not IsArray($res) Then Return SetError(0)
   Return $res
EndFunc
Edited by mikell

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