Ask coding questions

← Back to all posts
Are Regular Expressions in Test Cases for Class Assignments Working?
JohnDutton (4)

I am trying to use regular expressions in test cases and I am unable to get anything other than literal matching to work. Specifically, I am trying to check the output of:

print ("Hello World")

and I have set the regular expression to each of the following without success:

/Hello World/i
/Hello World/
new RegExp(/Hello World/i);

Is this functionality working and I am missing some documentation?

daveavis (4)

This has been driving me crazy, too, and I finally figured something out. I've gotten regular expressions to work, but I can't get flags to work.

In a Hello World assignment I got the following regular expressions tests to pass. Expected output is "Hello world!"

It seems that regular expressions work if you don't put the leading and trailing "/". But, this leads to the issue of how to add flags. I'd really like to be able to add flags like "i" for case-insensitivity. Here are some things I tried that didn't work. Maybe someone can figure something out.
hello world!/i
hello world!//i
hello world!\/i
"hello world!"/i
"hello world!",i
RegExp("hello world!", "i")
RegExp('hello world!', 'i')
new RegExp('hello world!', 'i')

mat1 (3308)

Hey, can you please give us a Repl link so we can help you?
If this is JavaScript, then you need to put a semicolon at the end of the line.
If this is Python, then regexes don't work like that so here's some quick documentation for you.

Please upvote my comment if it was helpful or useful in any way

JohnDutton (4)

@mat1 This is inside of a test case for a class assignment. It seems that the test cases allow for JS inside since it references in the help. But I've tried every possible permutation of the regular expression to get the test case to work with no luck.

Edit: To clarify, I can't provide a Repl link because it's not on a project, it's about a feature of Repl itself.

n_frost (2)

@JohnDutton Did you ever receive a reply or do you have any idea how to fix, as I can't get it working either (and the 'example' they give doesn't work).

JohnDutton (4)

@n_frost I have received neither. Good to know I'm not the only one!

darrylluther (2)

How did this get marked as an answer? It doesn't address the question at all, let alone answer it.

NicolasMcIntyre (0)

I just ran into this problem while setting up my AP CS A class and it made me feel dumb. 🙁

One way to make regex work properly is to explicitly mark the start ^ and end $ of the string your pattern is to match.

^Hello, World!\n[a-zA-Z0-9]+$

No forward slashes /.

The example is me figuring out how to test Exercise 2 from Chapter 1 of Think Java 2e by Allen Downey.

rammonsnova (0)

Regular expressions don't work properly (or not at all). I've tried several variations, with/without start/end "/" markers as well as lots of others...all based on Mozilla ECMA regex, as referenced in your documentation.

I have noticed that if I put incorrect regex (i.e. invalid), the system will hang when running the test. So, it looks like it's running SOME kind of regex algo...but it's just very poorly implemented/documented.

Please fix this as it is a vital part of your platform for us instructors.

I've taken most of regex out, and will likely delete the specific test so it doesn't cause a failure for my students...but here's the link so that an admin can look at the assignment and run a test regex.

You can see, however, in the image below that it is treating the regex characters as least in how it's displaying output:

gideonyuval (0)

I'm seeing the exact same thing. What works is only what @daveavis wrote below. I can't get flags to work