-1

So I have a string, "this-is-a-big-tool" and swap out THIS and TOOL for different words but maintain BIG

import re
test = "this-is-a-big-tool"
s = [("a","b"), ("a","d"), ("c","d")]
for a,b in s:
    result = re.sub("this-[\w]+-[\w]+-[big|giant]-tool", "%s-moves-big-%s" % (a,b), test)
    print(result)

The issue is that say the only thing I care about is THIS, BIG, TOOL. I want to swap THIS and TOOL but keep BIG. and I dont care about the other words.

So my goal is to do something like:

a-is-a-big-b
a-is-a-giant-d
c-is-a-giant-d

The issue is that i figured out the regex, but how to i pass BIG or GIANT into the replace portion of the code?

result = re.sub("this-[\w]+-[\w]+-[big|giant]-tool", "%s-moves-big-%s" % (a,b), test)
               How Do I pass This ---^                  into --^
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • How is it that less than half a second after i post, i get a Downvote? I have no clue what the downvote is even for. – Fallenreaper Feb 09 '18 at 22:42
  • Capture it in parens `([big|giant])` and reference the content of the match group in the replacement string with `\1`. e.g. e.g. https://stackoverflow.com/a/14007559/478656 ; No idea about the downvote, but maybe because you say "here's my problem, what's the solution?" with no discussion about what you tried and what happened? – TessellatingHeckler Feb 09 '18 at 22:46
  • I didn't downvote, but why `big` should be placed only in 1st item? what if there 5 tuples there? – RomanPerekhrest Feb 09 '18 at 22:46
  • I think because it is unclear what you want. I'm thinking: Why don't you use a formatted print statement? – broch Feb 09 '18 at 22:47
  • I mean, it was an option, I think maybe the `\1` is the test i am looking to pass the selection into the replace – Fallenreaper Feb 09 '18 at 22:53
  • @TessellatingHeckler I was testing this, and when I tried: `re.sub("this-is-a-([big|giant])-test","%s\1%s" % (a,b),test)` but it doesnt place BIG or GIANT in the resulting string – Fallenreaper Feb 09 '18 at 22:59
  • 1
    Darnit, raw string or double backslash before 1: `print(re.sub("([big|giant])-tool", r"\1-b", "big-tool"))` and `print(re.sub("([big|giant])-tool", r"\1-b", "giant-tool"))` – TessellatingHeckler Feb 09 '18 at 23:11
  • @TessellatingHeckler I was looking into this, and it works, though for some reason the brackets is having issues: `[]` . So when i did: `(big|giant)?-tool` it was giving me the results I was wanting. Part of me was thinking brackets was needed to group the items, but the way i printed it got the right regex and your: `r'\1-b` does the trick to copy it. – Fallenreaper Feb 12 '18 at 15:14
  • @TessellatingHeckler but i also noticed that Python will throw an error if say big|giant doesnt exist and I attempt to reference it with the \1. I was thinking that \1 was going to just be the empty string, but it throws an exception. – Fallenreaper Feb 12 '18 at 15:34

1 Answers1

0

You can try this:

import re
test = "this-is-a-big-tool"
s = [("a","b"), ("a","d"), ("c","d")]
new_results = [re.sub('this|tool', '{}', test).format(*i) for i in s]

Output:

['a-is-a-big-b', 'a-is-a-big-d', 'c-is-a-big-d']
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • This isnt quite right. So starting with "this-is-a-big-tool" as a string, I want to be able to replace THIS and TOOL, while maintaing the BIG|GIANT, so that way, it would replace like what you did but also maintain whether the adjective is BIG or GIANT as well. In the comments, you can see it boiling down to the fact of "How do I scrape something out of the first part of `re.sub` and put it into the second argument – Fallenreaper Feb 11 '18 at 14:54