-1

What can I use for conditional replacements for regular expression objects in regex python just like this javascript code?

var string = "heLlo, woRlD!";
string = string.replace( /([a-zA-Z])([a-zA-Z]+)/g, function(match, g1, 
g2) {
return g1.toUpperCase() + g2.toLowerCase();
    }); 
console.log( string ); // "Hello, World!"

I want to replace every pattern matches in the sentences. In regex pattern, there are two conditions and if we output re.groups, there are 2 groups and I want to perform another regex operation and then replace these new transformed groups with the matched parts in original sentence. It's operation is the same as this javascript code describes but only the operation to groups is different. Do you have any recommendation?

Htut Lin Aung
  • 31
  • 1
  • 6
  • something like this? https://stackoverflow.com/questions/18737863/passing-a-function-to-re-sub-in-python – Sundeep Jan 29 '19 at 08:58

1 Answers1

0

That Javascript uses (or one Javascript programmer chose to use) regular expressions for this is not a good reason to take the same approach in Python. Look instead at the intent of the original and reimplement.

>>> string = "heLlo, woRlD!"
>>> print (string.title())
Hello, World!
BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • I want to replace every pattern matches in the sentences. In regex pattern, there are two conditions and if we output re.groups, there are 2 groups and I want to perform another regex operation and then replace these new transformed groups with the matched parts in original sentence. It's operation is the same as this javascript code describes but only the operation to groups is different. Do you have any recommendation? – Htut Lin Aung Jan 29 '19 at 14:15
  • 1
    I suggest you transfer all of that comment into the body of your question and give a couple of examples. With only a single hard-coded example it was not possible to tell that you had a very generic problem in mind. Also remember that people who can offer expert Python help may not all be Javascript wizards. You may need to explain what the Javascript code does. – BoarGules Jan 29 '19 at 14:31
  • Thanks. It's kind hard to explain because I am performing normalization task on the sentences,in my language. It's exactly same as these js script. In the matched patterns(word), I take the first character and then I perform some sorting to the rest of the characters. Then merge it back and use that newly merged word as replacing material for the matched pattern. But if I don't use replace, then the result would be only regex matched patterns. There are characters in the sentence which I want to ignore in regex operation but want to attach them to final output. Sorry for the inconveniences! – Htut Lin Aung Jan 29 '19 at 15:03
  • Please don't amplify your question in comments. *Edit your question* to include all this stuff you didn't explain the first time around. Responders are not going to comb through the comments and synthesize them into a question they can answer. They will just move on to the next question. – BoarGules Jan 29 '19 at 15:22