Ask coding questions

← Back to all posts
Help with regex
Baconman321 (81)

I tried running this and inputted 5-5, but it said there was an unknown modifier: '*' in line 5. Please try running this for yourself and explaining what went wrong. Thanks

Answered by theangryepicbanana (1628) [earned 5 cycles]
View Answer
Commentshotnewtop
theangryepicbanana (1628)

First of all, you can replace the {1,} with a + since they do the same thing. Second of all, I don't seem to get any errors

Baconman321 (81)

@theangryepicbanana It gets a warning, and doesn't return the result.

theangryepicbanana (1628)

@Baconman321 I did say to replace the entire {1,} with a +, not {1+} (because that actually is an error). how much regex do you know?

Baconman321 (81)

@theangryepicbanana Not much, however I am following the link you sent me. I am also kinda getting it. Also I replaced it, and it doesn't work.

theangryepicbanana (1628)

@Baconman321 ok so there are 2 more things. 1) since you are putting the regex in a string, you don't need the / at the beginning and end of the regex. 2) in the [+/*-\], you should escape the - using a slash like \-, not -\

Baconman321 (81)

@theangryepicbanana The / is for division. Thanks though, the undefined modifier warning went away. However, now it is saying that there is no ending delemiter ^.

theangryepicbanana (1628)

@Baconman321 that's because you need to change [+/*-/] to [+/*\-] because the characters -^[] have special meaning inside a character class (the [...]). In order to use those characters in a character class, you need to prefix them with a \ so that the regex knows to not use its special meaning

Baconman321 (81)

@theangryepicbanana I did that, and it still has that error.

theangryepicbanana (1628)

@Baconman321 hmm. try escaping the * as well? I thought most regex was ok with a plain * inside a character class but maybe php doesn't like it?

theangryepicbanana (1628)

@Baconman321 so after looking into php I found out that you do need the beginning and ending /, and you also need to escape the / in the [...] as well. here's an example https://www.phpliveregex.com/p/uSg

Baconman321 (81)

@theangryepicbanana Now it says there undefined moderator \

theangryepicbanana (1628)

@Baconman321 you still have an extra (and unescaped) / in [+/\/*\-]. it should be [\+\/\*\-]

Baconman321 (81)

@theangryepicbanana Thank you so much. However, when I compile it, and input 5+5, it outputs 0. Is there something I'm doing wrong?

theangryepicbanana (1628)

@Baconman321 I believe that the result of a preg_match returns whether or not it matched successfully. according to the docs, you need to pass an array variable as a third argument to get the results (which will be stored in said array)

Baconman321 (81)

@theangryepicbanana Ok I did it, and it printed 5+5. That's definitely better. However, I would like to print the answer:10, not 5+5. Can I get some help?

theangryepicbanana (1628)

@Baconman321 try using capture groups (if you've learned about those) in order to access the first nimber, the operation, and the second number individually (hint: (...) will capture whatever is inside it as a single group)

Baconman321 (81)

@theangryepicbanana So how would I do this? I don't know anything about capture groups.

theangryepicbanana (1628)

@Baconman321 read more about them in the tutorial I gave you

Baconman321 (81)

@theangryepicbanana Can we continue this on discord (if you have it), just so it is easier to communicate. Thanks. My name is Neutrino#6688.

theangryepicbanana (1628)

@Baconman321 you could always join the repl.it discord instead since I'm also on there

Baconman321 (81)

@theangryepicbanana Ok so, I got an equation: 5-5 into an array. The "0" value is 5-5, the "1" value is 5, the "2" value is -, and the "3" value is 5. I just need to know how to get the computer to evaluate this as 1 variable, and print the result: 0. Is this possible? Also if so, how do I do it? Thanks.