1

Quite new to regular expressions and am trying to get a grasp on them

string = "regex_learning.test"
subbed = re.sub(r'(.*)_learning(.*), r'\1', string)

What I was hoping for is "regex.test" as an ouput when printing subbed, however I just get "regex"

Could someone explain why I am losing the .test?

Thanks in advance

1 Answer 1

2

Use this:

subbed = re.sub(r'(.*)_learning(.*)', r'\1' + r'\2', string)

You can also write it as:

subbed = re.sub(r'(.*)_learning(.*)', "%s%s" % (r'\1', r'\2'), string)
Sign up to request clarification or add additional context in comments.

4 Comments

Could you explain what is going on with the + r'\2' there? It helps if I can follow along what the script is saying
\1 gives you the match captured by the first (), and \2 gives you the match captured by the second (). The latter captures the .text part of your string. You want the substituted string to be concatenated from the two matches.
2 mins and counting! Is there a better way to do this than (.*)?
@user3234810 : Rather than using *, it's generally better to use *? which gives you non-greedy matching. So (.*) would become (.*?) See *?, +?, ?? in 7.2.1. Regular Expression Syntax.

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.