3

I am creating rules for a reddit automoderator. It gets its rules from a YAML config file and the regexes are interpreted as Python regex.

I am trying to make the following regular expression work:

(https?://[\\w\\d:#@%/;$()~_?+-=\\.&]+\\.\\w{2,6})([\\S\\s]*\\1)

When I test it on https://pythex.org/ it works perfectly to achieve what I want.

Unfortunately my group reference at the end of the expression is causing an error when I copy the same regex into the config file:

Generated an invalid regex for body (regex): cannot refer to an open group

I have also tried this version with everything escaped just to make sure that the characters weren't interfering in any way:

(https?://[\\w\\d\\:\\#\\@\\%\\/\\;\\$\\(\\)\\~\\_\\?\\+\\-\\=\\.&]+\\.\\w{2,6})([\\S\\s]*\\1)

But I still get the same error. Does anyone know what I'm doing wrong here?

8
  • Can you try to print the regex? Also, try it without the ( [\S\s]* \1 ) and see if it errors. Commented Nov 11, 2018 at 19:23
  • I cannot print the regex, as I am just setting rules in YAML and they are being interpreted by someone else's python code. It does not have errors without the end group. Commented Nov 11, 2018 at 19:28
  • That error is thrown if you reference a group inside the group like this so it could be the closing ) is not being seen. Commented Nov 11, 2018 at 19:29
  • Then, remove the end group and just put \1 at the end, as a test. Commented Nov 11, 2018 at 19:30
  • And, it wouldn't hurt to escape the forward slashes for a test ((https?:\/\/[\w\d:#@%\/;$()~_?+\-=\.&]+\.\w{2,6})([\S\s]*\1) Note that you have to escape the dash in the class if you want it to be literal \- Commented Nov 11, 2018 at 19:34

1 Answer 1

6

I managed to fix the problem by changing the group selection to \2 instead of \1.

It turned out that YAML or AutoModerator were automatically putting parentheses around the whole expression, so any group references within must be 1 more than you would initially expect.

I had thought that this was the problem at the start, and tried the fix explained above, however due to a separate issue with the AutoModerator code, the fix had not appeared to have worked. All resolved now though; thanks for your patience and help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.