Isolating match from a string RegexMatch

0 favourites
  • 10 posts
From the Asset Store
snake and ladder game with two characters: turtle and rabbit!
  • I want to isolate commands from a string I have multiple commands in the same string and thought that regex was the best choice to isolate them from a string:

    from:

    "Hello[command]function1[/command] how are you doing?[command]function2[/command]"

    to:

    index 1-"[command]function1[/command]"

    index 2-"[command]function2[/command]"

    Can someone tell how to achieve this result? I can't make it stop on the first command.

  • I don't know regex that well, but I know how to do this with tokenat and tokencount:

    For x=1 to tokencount(sourceString, "[command]")
     Set s = tokenat(sourceString, loopindex, "[command]")
     Set functionName to tokenat(s, 0, "[/command]"
     Array push functionName 
    
  • I wanted to try regex because it would let me do it in 1 line and I totally forgot about tokens, but this works fine thanks.

  • This Regex would have done the trick also :

    [(\w+)\](.+?)[\/\1\]

  • Thanks this is just what I wanted, you misplaced some characters but I got it to work properly:

    Edit:

    Although on a second thought I think I want to isolate just the content of the command ignoring the [command][/command] like in Dop2000's example

  • Weird, it seems some escaping characters were left out when I copy/pasted from RegExr. (That tool is invaluable!)

    To isolate what's between your tags you need to inspect capture group #2 as seen here :

    However, Construct implementation of Regex is a bit lacking in that regard, you can't inspect capture groups while in global mode. You need to rexecute the Regex on each match individually without the "g" flag, which is quite a bother.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks, I got it all working and I'm tweaking it at pleasure, it's just a bummer that construct can't make better use of regex because I think it's pretty useful and I'm liking it so far.

  • Magistross what exactly is RegEx? I know it stands for 'regular expression'. But is it native java or rather a shorthand? I don't understand the syntax except for some small things I picked up here and there... '?' is an if condition etc... where do you recommend I learn more about it?

    I looked at RegExr.com and I don't even understand what it is...

  • jobel Most programming language implements a "regular expression" interpreter that share more or less the same syntax... so one could say that regular expression is a language agnostic way to search for patterns in some text. It can be used for things like advanced searches\replaces, validation and so on.

    If you're curious about it, this site should have you covered : regular-expressions.info/index.html

  • Magistross I remember years ago when I used to write shell scripts in (Bourne shell I think) I would do:

    expr `1+1` (with tidles) or something like that..with grep maybe? I forget! but this seems different. I only know lower level programming languages maybe that's why its unfamiliar to me.

    Thanks for the link!

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