-1

I'd like to replace quantities with name then a square bracket and a single quote with the contents inside. So, from this:

RSQ(name['BAKD DK'], name['A DKJ'])

to this:

RSQ(BAKD DK, A DKJ)

user1387717
  • 1,039
  • 1
  • 13
  • 30

2 Answers2

5

Code -

import re
s = "RSQ(name['BAKD DK'], name['A DKJ'])"
expr = r"[\'\[\]]|\bname\b"
print(re.sub(expr, '', s))

Output -

RSQ(BAKD DK, A DKJ)
Vedang Mehta
  • 2,214
  • 9
  • 22
  • 1
    I don't think this expression is reliable. Imagine you have `something(name['BAKD DK'], name['A DKJ'])` input string. You'll get `sothig(BAKD DK, A DKJ)` as a result - clearly not what the OP asked about. – alecxe May 22 '16 at 15:06
  • Okay, but now, what if we would have `name(name['BAKD DK'], name['A DKJ'])`?..anyway, if the answer is accepted then it probably fits the OP's needs at the moment. Removed the downvote. Thank you. – alecxe May 22 '16 at 15:22
  • The OP did not explicitly mention what "name" he wants to remove. If he does, I'll change my answer. Thanks for the previous suggestion :) – Vedang Mehta May 22 '16 at 15:25
2

You can also use the saving groups to extract strings from inside the name['something']:

>>> import re
>>> s = "RSQ(name['BAKD DK'], name['A DKJ'])"
>>> re.sub(r"name\['(.*?)'\]", r"\1", s)
'RSQ(BAKD DK, A DKJ)'

where (.*?) is a capturing group that would match any characters any number of times in a non-greedy fashion. \1 references the captured group in a replacement string.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • just out of curiosity, what would be the regex for changing given input to `XYZ(BAKD DK, A DKJ)` – rock321987 May 22 '16 at 15:09
  • @rock321987 sure, if you mean replace the `RSQ` with `XYZ` as well, I'd probably do it in a separate `re.sub()` call - something like `re.sub(r"^(\w+)(?=\()", r"\1", s)`. – alecxe May 22 '16 at 15:12
  • yeah..well i am curious to know whether it can be done in single regex? – rock321987 May 22 '16 at 15:15
  • @rock321987 interesting question. I'd say if the number of `name['something']` cases is known beforehand (like in this case, it is 2), then, we can just do `re.sub(r"^(\w+)\(name\['(.*?)'\], name\['(.*?)'\]\)$", r"XYZ(\2, \3)", s)`. Which would produce `XYZ(BAKD DK, A DKJ)`. – alecxe May 22 '16 at 15:19
  • that can be possible..but what if its not known beforehand? this question is bugging me for few days – rock321987 May 22 '16 at 15:20
  • @rock321987 yeah, that's definitely a nice brain teaser! I think `regex` module should help in this case..created a separate thread - http://stackoverflow.com/questions/37381249/replacing-repeated-captures. Thanks! – alecxe May 23 '16 at 01:02
  • @thumbs up for creating the thread..though, i was interested in generalized `pcre` approach..means whether it was possible to skip some part of regex(after first match) and continue to check from the next part..it sounds silly(and I believe it is), but I think its not possible..taking help of language will be better – rock321987 May 23 '16 at 03:10
  • 1
    @rock321987 yeah, the question I've asked is not generic and quite tied to a specific use case we've got here. After couple hours of trying to trick the regex engine (both the `re` and `regex`) and judging by the current answers in that thread..it looks like there is no other way as to use the language, at least minimally..and, if we account the simplicity and readability (which counts) it'd better to just apply multiple sub calls. Thanks for an interesting challenge! – alecxe May 23 '16 at 03:36
  • exactly..for simplicity, I always prefer multiple regex..its easier to understand and implement for anyone.. – rock321987 May 23 '16 at 05:08