How do I retreive specific part of a textfile?

0 favourites
  • 4 posts
From the Asset Store
Get isometric castle and wall parts rendered in 8 directions in the angle of 33 degrees.
  • 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!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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 "!"

  • 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):

    However, this would allow you to specify the number:

  • Thanks R0J0hound, it works wonders and that link was very helpful!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)