How do I RegexMatchAt correctly?

Get help using Construct 2

Post » Tue Jan 12, 2016 5:40 pm

Hi,

I want to make a RegexMatchAt that from an string results only one Regex... It will be more clear with an example.

I have one string: bu,si,tres

with this Regex: [^,]+ (It selects groups of any character separates with a comma ,)

So, if I understand well I will hope that:

RegexMatchAt("bu,si,tres","[^,]+","i",1) ==> bu
RegexMatchAt("bu,si,tres","[^,]+","i",2) ==> si
RegexMatchAt("bu,si,tres","[^,]+","i",3) ==> tres

But It sees none with 2 and 3... Can anyone help me?
B
8
Posts: 34
Reputation: 512

Post » Tue Jan 12, 2016 6:26 pm

RegexMatchAt("bu,si,tres","[^,]+","gi",0) -> bu
RegexMatchAt("bu,si,tres","[^,]+","gi",1) -> si
RegexMatchAt("bu,si,tres","[^,]+","gi",2) -> tres
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Tue Jan 12, 2016 6:42 pm

It's worth noting that RegexMatchAt behaves differently if the global flag is on or off. If on, the index argument will fetch the n-th match, if off, the index will fetch the n-th capture group (with index 0 being the whole match, index 1 the first capture group, and so on so forth). Haven't found a way to get capture groups yet when using the global flag, except by nesting two RegexMatchAt function calls...
B
64
S
27
G
24
Posts: 959
Reputation: 18,478

Post » Wed Jan 13, 2016 7:03 am

@mindfaQ Thanks.

@Magistross I'm sorry, but I don't understand a lot, can you explain it more?
B
8
Posts: 34
Reputation: 512

Post » Wed Jan 13, 2016 5:18 pm

Without "g" in the flags, it looks for the first match of the regex expression and returns it as index 0. If you set any () (brackets), which magistross called capture groups, you can access them with 1, 2, 3, ... (contents of first bracket, second bracket and so on).
With "g" you access all matches of the regex and can access them by index, BUT capture groups can't be accessed (because the index is already used for selecting the match itself (like what you want to happen)).
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Wed Jan 13, 2016 7:18 pm

@mindfaQ pretty much nailed it! I'm not sure if it's an intended behavior or not... however when you know about it, you can combine two RegexMatchAt to fetch capture groups even with the global flag set.
B
64
S
27
G
24
Posts: 959
Reputation: 18,478

Post » Wed Jan 13, 2016 9:34 pm

Thanks a lot @mindfa and @Magistross

I think I understand it better know.
B
8
Posts: 34
Reputation: 512


Return to How do I....?

Who is online

Users browsing this forum: conker23, Davlin and 4 guests