[SOLVED] Testing if a string matches a regular expression

RedTulip
09 Mar 2015, 06:43
Hiya. It's been a while. x) Right now, I'm having a problem testing if a player input matches a regular expression. I've tried adding a JavaScript script and then entering
var theaterBool = /^\d{4}$/.test(theaterAns);
That doesn't seem to work.

Every time I save and try to run it, the JavaScript code changes to
var (theaterAns)
.

So I'm just wondering if there's any way to check if a string matches a regular expression. Btw, I'm using the online version of Quest, 'cause right now I'm on a linux computer. Thanks! :D

jaynabonne
09 Mar 2015, 07:44
Perhaps this will work: IsRegexMatch

http://docs.textadventures.co.uk/quest/ ... match.html

RedTulip
09 Mar 2015, 08:53
Thanks for the quick reply! :) I tried adding the script that calls the function IsRegexMatch with the parameters /^\d{4}$/ and theaterAns, but when I tried playing the game and testing the script, there was an error: "Error running script: Missing ')'".

Is there any other way I should enter the regular expression?

Here's a pic for reference.

And of the problem...

Silver
09 Mar 2015, 09:22
Isn't this an easier way?

msg("what is the password?")
Get input {
If { result = ("1234")
// run password correct script
} else {
// run password incorrect script
}
}

Silver
09 Mar 2015, 09:32
Not sure if I've done that code entirely right lol.

Pertex
09 Mar 2015, 09:47
Could you click the button "code view" and post a picture of the code please?

RedTulip
09 Mar 2015, 09:51
Silver wrote:Isn't this an easier way?

msg("what is the password?")
Get input {
If { result = ("1234")
// run password correct script
} else {
// run password incorrect script
}
}

Yeah, I guess so. But I'm trying to get it so that the keypad only accepts 4 digits, and if they type anything other than that it should come up with an error like, "The keypad only accepts 4 digits."

But using your code with a general error, something like "That doesn't work", would be easier. I do want to know how to properly use the "IsRegexMatch" function, though.

Pertex wrote:Could you click the button "code view" and post a picture of the code please?


Pertex
09 Mar 2015, 10:00
I think the pattern must be quoted: "/^\d{4}$/"

Silver
09 Mar 2015, 10:01
RedTulip wrote:
Yeah, I guess so. But I'm trying to get it so that the keypad only accepts 4 digits, and if they type anything other than that it should come up with an error like, "The keypad only accepts 4 digits."


Ah right, I didnt think of that.

Silver
09 Mar 2015, 10:03
there's probably a way to check if a string is over four characters.

RedTulip
09 Mar 2015, 10:11
Pertex wrote:I think the pattern must be quoted: "/^\d{4}$/"

Ah. I'll try that.

Silver wrote:there's probably a way to check if a string is over four characters.

I guess I could use LengthOf and IsInt... I'll check if Pertex's reply works, first.

Silver
09 Mar 2015, 10:17
Sorry, I'm forever thinking out loud. :oops:

RedTulip
09 Mar 2015, 10:36

Odd. :/

Silver wrote:Sorry, I'm forever thinking out loud. :oops:

Lol. x)

The Pixie
09 Mar 2015, 10:48
Pertex wrote:I think the pattern must be quoted: "/^\d{4}$/"

You also need to remove the / delimiters. And you also need to escape the backslash.
msg(IsRegexMatch("^\\d{4}$", "1234"))
msg(IsRegexMatch("^\\d{4}$", "12345"))
msg(IsRegexMatch("^\\d{4}$", "1w234"))

This will print:

True
False
False

RedTulip
09 Mar 2015, 10:52
Tried using an if expression instead.

Here's the code view, if you want:
theaterAns = result
if (IsRegexMatch ("/^\d{4}$/", result) = true) {
msg ("Yay!")
}
else {
msg ("Still yay!")
}


And now this error comes up after entering some digits:

The Pixie
09 Mar 2015, 11:17
Try this:
theaterAns = result
if (IsRegexMatch ("^\\d{4}$", result) = true) {
msg ("Yay!")
}
else {
msg ("Still yay!")
}

Quest handles RegExs as strings, so you need to delimit the RegEx with double quotes, but therefore you do not need the forward slashes. Also, because this is a string, where backslash indicates an escape code, you need to escape the backslash with a second backslash.

RedTulip
09 Mar 2015, 11:28
The Pixie wrote:Try this:
theaterAns = result
if (IsRegexMatch ("^\\d{4}$", result) = true) {
msg ("Yay!")
}
else {
msg ("Still yay!")
}

Quest handles RegExs as strings, so you need to delimit the RegEx with double quotes, but therefore you do not need the forward slashes. Also, because this is a string, where backslash indicates an escape code, you need to escape the backslash with a second backslash.


Thank you so much! This worked. I'll go mark this solved. :) Also, thanks to everyone who's replied. <3

Edit: Sorry, I seem to have overlooked your previous post. Just saw it right now. xD

HegemonKhan
09 Mar 2015, 17:57
within quest, you can do it this way too (as this 'regex' and etc code symbol fancy stuff is still too confusing for me ~ I'm a noob trying to learn to code, hehe), for example:

Quest's 'Coding Bible' links:

http://docs.textadventures.co.uk/quest/

http://docs.textadventures.co.uk/quest/ ... notes.html

http://docs.textadventures.co.uk/quest/ ... quest.html
http://docs.textadventures.co.uk/quest/ ... ments.html

http://docs.textadventures.co.uk/quest/elements/
http://docs.textadventures.co.uk/quest/types/

http://docs.textadventures.co.uk/quest/functions/ (category order)
http://docs.textadventures.co.uk/quest/ ... tions.html (alphabetical order)
http://docs.textadventures.co.uk/quest/scripts/

yes, as you already found out, there's: LengthOf ( http://docs.textadventures.co.uk/quest/ ... gthof.html )

<function name="first_name_function">
msg ("What is your (first) name?")
msg ("(10 characters max)")
get input {
// quest's engine automatically (hiddenly) sets a built-in variable for you when using the 'get input' and 'show menu' Functions: result = your_typed_in_input
if (LengthOf (result > 10) ) {
ClearScreen
first_name_function
} else {
player.first_name_string = result
ClearScreen
}
}
</function>


--------------------------

'IsInt' is just a check upon whether the text is able to be an Integer Attribute:

text~variable's value: 1 -> yes, this CAN be converted into an Integer Attribute
text~variable's value: 1.5 -> no, this can NOT be converted into an Integer Attribute
text~variable's value: m -> no, this can NOT be converted into an Integer Attribute

<function name="age_integer_function">
msg ("What is your age?")
get input {
if (IsInt (result)) {
player.age_integer = ToInt (result)
} else {
age_integer_function
}
}
</function>


so, it as seen above, has nothing to do with an input's character~symbol length.

------------------------------

@Silver:

you're getting a bit confused with the text processor format and regular quest format

Silver: incorrect syntax
msg("what is the password?")
Get input {
If { result = ("1234")
// run password correct script
} else {
// run password incorrect script
}
}


correct syntax:

Script_or_Function_command (if needed: Object_name.Attribute_name OPERATOR Value_or_Expression) { scripting }

msg("what is the password?")
Get input {
If (result = "1234") {
// run password correct script
} else {
// run password incorrect script
}
}

RedTulip
09 Mar 2015, 18:53
^^
Thanks for the input. x) I needed IsInt 'cause the key pad is only supposed to accept four numbers.

Regular expression does work better, the script ends up quite a bit shorter.

Silver
09 Mar 2015, 19:03
Yeah it's not quite muscle memory yet, HK. Because the curly braces came straight after the else I assumed it comes after the if, but that ignores the condition.

HegemonKhan
09 Mar 2015, 21:33
@Silver:

I know, just trying to help (re-in-force) with that muscle memory ;)
(my apologizes if it came off as nagging~correcting, didn't think about this before I posted it, my bad)

(Someday, I'll get the muscle memory for these real languages that use all these symbols~characters, to shorten the amount of coding~writing needed, as RedTulip+Pixie+Jay+etc real coders, understands, but right now, it's totally alien for me, even if I understand that the symbols~characters are just variables... it's just too much for my brain to remember, understand, and keep straight. So, for now, I still really like quest's XML, as it doesn't use that many symbols~characters, and thus I've been able to understand and learn it, and thus the basics of coding in general as well, hehe)

Silver
09 Mar 2015, 21:47
Nah, I know you're not nagging. :) Or, if you are, someone has to! :lol: I'm at the stage, and will be for a while, where bad habits can creep in.

HegemonKhan
09 Mar 2015, 23:25
RedTulip wrote:Thanks for the input. x) I needed IsInt 'cause the key pad is only supposed to accept four numbers.


indeed for that scenario, using quest's coding, you'd need both actually:

'IsInt' + 'LengthOf'

an example of another scenario that would need both of them:

<function name="year_of_birth_integer_function">
msg ("What is your year of birth?")
get input {
if (IsInt (result) and LengthOf (result) = 4) {
player.year_of_birth_integer = ToInt (result)
ClearScreen
} else {
ClearScreen
year_of_birth_integer_function
}
}
</function>


-----

RedTulip wrote:Regular expression does work better, the script ends up quite a bit shorter.


ya, 'regex' and other languages' symbol~character usage is much better~efficient (less writing~coding), once you learn it well (unlike me currently).

-----

quest does also have:

IsInt (specifically for checking on the, text ~ variable's value, being integers: -1, 0, 1): http://docs.textadventures.co.uk/quest/ ... isint.html

IsDouble (Double=Float=Floating Point = decimal numbers, same as 'IsInt' otherwise): http://docs.textadventures.co.uk/quest/ ... ouble.html

IsNumeric (this is a bit confusing with 'IsInt'. As 'IsNumeric' is basically: 'is non-alphabetical', though I'm not sure if it can check 'two' as being 2): http://docs.textadventures.co.uk/quest/ ... meric.html

ToString: http://docs.textadventures.co.uk/quest/ ... tring.html

ToInt: http://docs.textadventures.co.uk/quest/ ... toint.html

examples of the 'To' converters:

player.strength_integer = 100
player.strength_string = "100"

player.strength_integer = ToInt (player.strength_string)
player.strength_string = ToString (player.strength_integer)

and etc cool stuff (quest is a very well built engine, hehe)