How do I retreive specific part of a textfile?

Get help using Construct 2

Post » Sun May 17, 2015 9:23 pm

Hi! I am trying to retreive text from a txt file and display the content on the screen. I'm using AJAX to retreive the content of the .txt file and it works fine but I'm at a loss as for how to retreive a specific part of the textfile.

Say my textfile look like this:

"
01 text!
02 another text!

"

How can I retreive only the part that starts with 01 and stop reading after the exclamation mark?

Thanks!
B
20
S
8
G
4
Posts: 195
Reputation: 3,555

Post » Sun May 17, 2015 9:52 pm

Use a regex expression to do that:
RegexMatchAt(AJAX.LastData, "01.*!", "", 0)

Regex is very powerful and allows you to do a lot of cool text searching.
https://developer.mozilla.org/en-US/doc ... xpressions

This is the expression I used:
01.*!

What it does is:
0 matches "0"
1 matches "1"
. matches anything
* matches any number of the previous. Or in this case anything
! matches "!"
B
91
S
31
G
102
Posts: 5,232
Reputation: 67,250

Post » Sun May 17, 2015 9:57 pm

One way you could do it, is by removing the 01 and ! and just putting each message on a new line, then use the tokenat system expression with the delimiter being the newline thing (character?). Or, you could use a regex, in a general case, this regex would work: "[\d]+\s([\w\s]+\!)". For the purpose of extracting a string, if you know the number of the text you want, have that number stored, as a variable or parameter and then this regex would work: number & '\s([\w\s]+\!)'".

So this: would always return the first regex match (text):
Image

However, this would allow you to specify the number:
Image
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Mon May 18, 2015 1:35 am

Thanks R0J0hound, it works wonders and that link was very helpful!
B
20
S
8
G
4
Posts: 195
Reputation: 3,555


Return to How do I....?

Who is online

Users browsing this forum: tarek2 and 2 guests