Find all occurences of a string in a text?

Get help using Construct 2

Post » Mon Feb 09, 2015 8:42 pm

Ok, I can find the number of times a string occurs inside a text using the "RegexMatchCount" expression. But I can't find how to get every occurence's index inside the text.

Anyone knows how to do this?
B
11
S
3
G
3
Posts: 206
Reputation: 2,379

Post » Mon Feb 09, 2015 9:18 pm

Normally you would be able to get all match indexes, but Regex implemention in Construct is a bit lacking. You have to get creative. You will need to use RegexSearch recursively until no match is found, each time passing a substring of the original string that goes from the index of the last match + length of match, to the end of the string.
B
58
S
23
G
22
Posts: 890
Reputation: 16,790

Post » Mon Feb 09, 2015 9:35 pm

Maybe you could execute some JavaScript that does it.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Mon Feb 09, 2015 10:19 pm

:cry: Yeah, that's what I thought, but I hoped somebody would come with a simpler method. Oh well, it was worth the try.
Thanks for replying.

EDIT : Javascript, maybe. I'll check into that (or do you have a link to point me in the right direction? :) )
B
11
S
3
G
3
Posts: 206
Reputation: 2,379

Post » Mon Feb 09, 2015 10:27 pm

you can try with

> find(str(NumberORTEXT), "WhatYouSearchFor Must Match ")
Image
B
65
S
19
G
67
Posts: 1,310
Reputation: 42,040

Post » Mon Feb 09, 2015 10:32 pm

One of the solutіons in this thread should work.
http://stackoverflow.com/questions/4009 ... -in-string

then execute it
https://www.scirra.com/manual/110/browser
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Feb 10, 2015 12:06 am

See if this can help you :
You do not have the required permissions to view the files attached to this post.
B
58
S
23
G
22
Posts: 890
Reputation: 16,790

Post » Tue Feb 10, 2015 2:19 am

@gamescorpstudio : The "find" expression would only return the index of the first occurence of the string inside the text, wouldn't it?

@codah : Thanks... I just looked over those solutions but I don't understand javascript enough to know what I'd be doing. Beside, I think that they only show methods to retrieve the number of times a string occurs in a text, and not the index of every occurence of that string. I might be wrong though, since as I said I don't understand enough of it to know what's really going on in the code. :|

@Magistross : Thanks, your example works fine. I get that I need to insert the string I'm looking for between the two \w* brackets. But I start being lost the moment I look at the javascript. I'd like to implement in a way so it uses or stores the occurences' indexes it finds instead of showing them to me in an alert message, but I don't know how.
B
11
S
3
G
3
Posts: 206
Reputation: 2,379

Post » Tue Feb 10, 2015 2:47 am

Try this, no javascript. Stores the indices in a csv text variable.
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Feb 10, 2015 5:23 pm

@codah This is a great way of doing it, thanks. :) And it seems pretty fast even when going through longer texts.

I've simplified it a bit and I think that's what I'm gonna be using then. Thank you.
B
11
S
3
G
3
Posts: 206
Reputation: 2,379

Next

Return to How do I....?

Who is online

Users browsing this forum: LukeW and 11 guests