How to use Regex?

Get help using Construct 2

Post » Thu Aug 01, 2013 9:37 am

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!Joannesalfa2013-08-01 09:38:15
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,361

Post » Thu Aug 15, 2013 5:07 am

Bump, everyone should know how to use this feature.
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,361

Post » Fri Aug 16, 2013 9:03 pm

Bump
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,361

Post » Fri Aug 16, 2013 9:52 pm

I'm going to poke around with it a bit too , could I see a capx of your implementation ?
B
42
S
17
G
17
Posts: 2,247
Reputation: 17,461

Post » Sat Aug 17, 2013 2:56 am

@Whiteclaws my .capx is pointless, its using a Text object to compare regex, it doesn't work anything :(Joannesalfa2013-08-17 02:57:41
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,361

Post » Sat Aug 17, 2013 4:16 am

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. R0J0hound2013-08-17 04:17:32
B
91
S
31
G
102
Posts: 5,232
Reputation: 67,250

Post » Sat Aug 17, 2013 4:25 am

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)blackhornet2013-08-17 04:29:17
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Sat Aug 17, 2013 7:59 am

@R0J0hound

I tried it with:



It returned "Hello23" Yeah it removed "1" as single character of number
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,361

Post » Sat Aug 17, 2013 8:07 am

@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?
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,361

Post » Sat Aug 17, 2013 3:52 pm

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.

RegexTesterblackhornet2013-08-17 16:45:26
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Next

Return to How do I....?

Who is online

Users browsing this forum: Lancifer, Mayfly, Yahoo [Bot] and 5 guests