How to use Regex?

0 favourites
  • 15 posts
  • Hi guys :)

    I'm trying to learn how to use Regex using this reference:

    http://regexone.com/

    Sometimes I got javascript errors... I tried to implement that in C2 editor, when I created "Hello123" text, then I did:

    RegexMatchAt("Hello",".", "\d", 0)

    to remove numbers, but it didn't work (Forgive me, It's wrong) I don't understand how regex works in C2.

    Would you explain me a proper implementation?

    Thanks!

  • Bump, everyone should know how to use this feature.

  • Bump

  • I'm going to poke around with it a bit too , could I see a capx of your implementation ?

  • Whiteclaws my .capx is pointless, it?s using a Text object to compare regex, it doesn't work anything :(

  • Try this to remove digits from a string.

    RegexReplace("Hello123", "\d", "", "")

    I haven't tested it but it should work.

    My sources of info were the system expressions section of c2's manual and some JavaScript regex guide I found.

  • I have no idea what the Flags parameter does, but the correct Regex parameter should do what you need.

    One possibility:

    RegexMatchAt("Hello123", "[a-zA-Z]*", "", 0)

    ... and another:

    RegexMatchAt("Hello123", "[^\d]+", "", 0)

  • R0J0hound

    I tried it with:

    <img src="http://i.imgur.com/DbHaTnT.png" border="0" />

    It returned "Hello23" Yeah it removed "1" as single character of number

  • blackhornet

    It definetly removed "123" as numbers and returned "Hello" so what happens if I put "123Hello", it returned empty.

    Second method you gave us, it worked including "Hello123" and "123Hello" What happens if we put "Hello123World" it returned "Hello" I was expected it would say "HelloWorld" isn't possible?

    Anyway, did you guys test "Test Regex" event?

  • OK, figured it out.

    1) you need flag = "g"

    2) MatchAt works on the index of the match. You'll need to iterate by using MatchCount to build the string back up, if that's what you want.

    RegexTester

  • Ah, the "g" flag does help. Why use RegexMatchAt() and then rebuild the string when you can just use RegexReplace() where everything is done in one call?

    This now removes all numbers and not just the first number.

    RegexReplace("Hello123", "\d", "g", "")

  • blackhornet

    Nice! It worked like a charm, thanks! :D

    R0J0hound

    Thanks! It worked, I think Regex is great to validate forms such as e-mail, telephone number, etc.

    I would keep this thread alive if we can add some examples to use regex for all purposes.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Wow this is awesome! I was looking for a way to strain out certain text from the user media speech recognition feature and founf regex in system expressions. I searched on forum of course and found here where i tested blackhornet's capx with

    regexlib.com/CheatSheet.aspx

    and got all excited. the cheat sheet helped me put what i wanted in the regex textbox. I was a little confused about flag "g" i see that it might be referring to global, like what is entirely in the input textbox... still not sure tho

  • so now im trying to match everything but "Hello" and "World" so to clarify i want output to read Hello World

    (Hello) worked for just Hello... step one lol

  • Hello|World = HelloWorld

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