Jump to content

(Solved)Parsing text file?


Recommended Posts

Hello, I have a text file that contains a messages between me and my friend.. What i want to do is to Read the contents in the file as the following:

Date Time From To Msg

 

For example:

27/4/2016 8:15 user1 user2 Hello

 

I want to read every message in the file and it's date,time,from and to. And do a loop to display them into an array. I tried with StringRegExp but i failed.

Is there anyway to do it?

Edited by AhmedV
Link to comment
Share on other sites

It could be something like this. Of course it's only an example

#Include <Array.au3>

Local $prefix[4] = ["Date:", "Time:", "From:", "To:"]
$txt = "27/4/2016 8:15 user1 user2 Hello, how are you ?"
$res = StringRegExp($txt, '(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\N+)', 3)
For $i = 0 to 3
   $res[$i] = $prefix[$i] & $res[$i]
Next
$res[4] = '"' & $res[$i] & '"'
_ArrayDisplay($res)

 

Link to comment
Share on other sites

On 4/27/2016 at 8:57 PM, AutoBert said:

StringSplit returns a array. Show code you tried and also a small example file with (anonymized) messages

Thank you mikell solved it. ^^

On 4/27/2016 at 10:36 PM, mikell said:

It could be something like this. Of course it's only an example

#Include <Array.au3>

Local $prefix[4] = ["Date:", "Time:", "From:", "To:"]
$txt = "27/4/2016 8:15 user1 user2 Hello, how are you ?"
$res = StringRegExp($txt, '(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\N+)', 3)
For $i = 0 to 3
   $res[$i] = $prefix[$i] & $res[$i]
Next
$res[4] = '"' & $res[$i] & '"'
_ArrayDisplay($res)

 

Thank you, That what i was looking for, but i really need to understand the StringRegExp line. Why you inserted "  (\S+) and \s+ " 4 times? Can you explain why and also what does each of them do? I know about \N that reads every char except newline.

Link to comment
Share on other sites

In the example I used

$txt = "27/4/2016 8:15 user1 user2 Hello, how are you ?"

There are 5 fields to grab in one sequence. Assuming that the 4 fields date/time/user1/user2 contain no space, you can use \S - everything except space (including newline) - , and \N to grab the rest up to the next newline
and with (\S+)\s+ you get only the content of the group  :)

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...