How do I RegexMatchAt correctly?

0 favourites
  • 7 posts
  • 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?

  • RegexMatchAt("bu,si,tres","[^,]+","gi",0) -> bu

    RegexMatchAt("bu,si,tres","[^,]+","gi",1) -> si

    RegexMatchAt("bu,si,tres","[^,]+","gi",2) -> tres

  • 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...

  • mindfaQ Thanks.

    Magistross I'm sorry, but I don't understand a lot, can you explain it more?

  • Try Construct 3

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

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

  • 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.

  • Thanks a lot and Magistross

    I think I understand it better know.

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