AhmedV Posted April 27, 2016 Share Posted April 27, 2016 (edited) 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 April 29, 2016 by AhmedV Link to comment Share on other sites More sharing options...
AutoBert Posted April 27, 2016 Share Posted April 27, 2016 I Usernames didn't have space use StringSplit Link to comment Share on other sites More sharing options...
AhmedV Posted April 27, 2016 Author Share Posted April 27, 2016 The file contains messages like above Date time user1 user2 msg I need to read all messages with all information for each message and display them in array like: Date: Time From:user1 To:user2 "Msg" Link to comment Share on other sites More sharing options...
AutoBert Posted April 27, 2016 Share Posted April 27, 2016 StringSplit returns a array. Show code you tried and also a small example file with (anonymized) messages Link to comment Share on other sites More sharing options...
mikell Posted April 27, 2016 Share Posted April 27, 2016 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 More sharing options...
AhmedV Posted April 29, 2016 Author Share Posted April 29, 2016 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 More sharing options...
mikell Posted April 29, 2016 Share Posted April 29, 2016 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 AhmedV 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now