Sign in to follow this  
Followers 0
superbosu

search a string and separate

20 posts in this topic

#1 ·  Posted (edited)

_FileReadToArray("c:\file.txt",$A)
For $x = 1 To $A[0]
$An = $A[$x]

Next
Hi all,

I hope somebody can help me.

this is what I want to do:

I have a txt file, and I want to find all words that begin with "GED" and put them on a new txt

the words that I want to find are like this : GED124356

the only part of the word that don't change is "GED", the following 6 numbers can change.

is there a way to do this thing?

I know how to read the file and put it on an array, but I don't know how to extract the word GED569406 out.

this can be the part to read the file:

Edited by superbosu

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

How about reading all the words into an array and comparing the left 3 characters of your word:

if StringLen($word,3)=GED then

add to the new text file

endif

Edited by Fran

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hello superbosu,

please take a look at StringInStr() function in the helpfile, with this you can find the "GED" substring in a string.

Edit: Or you could use FileRead to read the whole File into a variable and do a StringSplit($text_of_file, " ") to have an array with each word. ...

But on the other hand this may be very ineffective.... :)

regards,

Hannes

;)

Edited by Hannes123

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

please take a look at StringInStr() function in the helpfile, with this you can find the "GED" substring in a string.

or you can do it the proper way.. hehehe

Share this post


Link to post
Share on other sites

Hi Fran,

If StringLen($word,3)="GED" Then

will never become TRUe, because StringLen() returns an int value.

:)

I think you meant StringLeft()?

regards,

Hannes

;)


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

I think you meant StringLeft()?

It's monday :)

Share this post


Link to post
Share on other sites

mhhh maybe is quite difficult for me (i'm a newbie) or maybe I didn't understand well.

this is an example of the file.txt :

"Hi all,

how are you? can you import GED342546 on the system?

and also do the same thing with GED876453 please?

tnx

valeria "

I need to put GED342546 and GED876453 on a new_file.txt in this way:

GED342546

GED876453

I'dont understand how to do this :)

Share this post


Link to post
Share on other sites

This is a job for StringRegExp

William

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hi saywell,

you're right. :)

Read the whole file to a single string variable using FileRead()

then run

$array = StringRegExp($string,"GED[0-9]{6}",3)

Edit: The returning array will contain all matches. Attention this array is 0-based, you'll need UBound() to get all values.

Regards,

Hannes

;)

Edited by Hannes123

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

superbosu,

Try running this and see if you get the 2 GEDs into the array: ;)

#include <Array.au3>

$sText = "Hi all," & @CRLF & _
         "how are you? can you import GED342546 on the system?" & @CRLF & _
         "and also do the same thing with GED876453 please?" & @CRLF & _
         "tnx" & @CRLF & _
         "valeria"

$aGEDs = StringRegExp($sText, "GED\d{6}", 3) ; basically the other SRE but with a slightly different syntax

_ArrayDisplay($aGEDs)

If you do, then we need to investigate why you do not when reading the text from file. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

You need to open the file before reading it - from the helpfile:

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in 1 character at a time until the EOF is reached
While 1
    $chars = FileRead($file, 1)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Char read:", $chars)
Wend

FileClose($file)

William

[edit] - no I'm wrong. can open with filename.

Edited by saywell

Share this post


Link to post
Share on other sites

Hi M23 :) yes it works with your code the array pops up with the two GED....

then I tryed with this but nothing happens again ;)

maybe something is missing...

#include <Array.au3>


$string = FileRead("d:\file.txt")

$array = StringRegExp($string, "GED\d{6}", 3)

_ArrayDisplay($array)

Share this post


Link to post
Share on other sites

superbosu,

Then let us see if you are reading the file correctly - please run this:

$string = FileRead("d:\file.txt")

MsgBox(0, "Text", $string)

Are you reading the file text correctly? Do the GEDs appear in the text? Do both GEDs have exactly 6 digits? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

DOH!!!!! so stupid... my fault.. now I can see the array! the file is not GED123456 but GEDK123456 sorry, now with this code works :)

tnx a lot!

#include <Array.au3>
$file = FileOpen("D:\file.txt", 0)

$string = FileRead($file)
MsgBox(0, "Text", $string)


$array = StringRegExp($string, "GEDK\d{6}", 3)

_ArrayDisplay($array)

FileClose($file)

Share this post


Link to post
Share on other sites

superbosu,

Good. :)

Debugging is a pain - but if you do it logically, you usually get there in the end. :idiot:

Are there any other variations on the GED/GEDK letters? Is it always 6 digits following? We can adjust the SRE if required to fit. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

ok now the problem is another one...

on the new file d:\CR.txt I found only the second GEDK926080 why?

what is wrong?

#include <Array.au3>
$file = FileOpen("D:\file.txt", 0)

$string = FileRead($file)
MsgBox(0, "Text", $string)


$array = StringRegExp($string, "GEDK\d{6}", 3)

_ArrayDisplay($array)

FileClose($file)
for $i = 0 to UBound($array) - 1
    msgbox(0, "test " & $i, $array[$i])
    $file2 = FileOpen("D:\CR.txt", 2); 2 = Write mode (erase previous contents)
FileWriteLine($file2, $array[$i] & @CRLF)
FileClose("D:\CR.txt")
Next
Edited by superbosu

Share this post


Link to post
Share on other sites

mhh maybe the 2 = Write mode

Share this post


Link to post
Share on other sites

superbosu,

You are opening the file for overwriting inside the loop and so deleting the existing file on the second pass. ;)

Try it like this: :idiot:

#include <Array.au3>

; You can read a file directly - no need to open
$string = FileRead("D:\file.txt")
MsgBox(0, "Text", $string)

$array = StringRegExp($string, "GEDK\d{6}", 3)
_ArrayDisplay($array)

; Open the file
$file2 = FileOpen("D:\CR.txt", 2); 2 = Write mode (erase previous contents)
; Start the loop
For $i = 0 To UBound($array) - 1
    ; Write the lines
    FileWriteLine($file2, $array[$i] & @CRLF)
Next
; Close the file
FileClose($file2) ; Note you use the handle, not the file name

I made a few other comments you might like to read as well. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Many tnx M23 :) tnx to everybody for your help! appreciated

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