0 Favourites

How do I RegexMatchAt correctly?

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

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • mindfaQ Thanks.

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

  • 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 mindfa 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)
Similar Topics Posts Views Last Post
Unread hot topic
99 10,158
karshinkoff's avatar
karshinkoff
Unread hot topic
56 4,736
MPPlantOfficial's avatar
MPPlantOfficial
Unread hot topic
0 Favourites
Why is Stencyl doing better?
44 16,859
Potato's avatar
Potato