Jump to content

search a string and separate


 Share

Recommended Posts

_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
Link to comment
Share on other sites

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]
Link to comment
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 :)

Link to comment
Share on other sites

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]
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...