As @bereal commented use the Regular Expression module re.sub.

Here's a simple example

Python:

>>> import re
>>> re.sub(r'([^A-Z])([A-Z])', r'\1_\2', 'camelCase').lower()
'camel_case'

And just for kicks here's it on PHP too:

<?php
echo strtolower(preg_replace('/([^A-Z])([A-Z])/', '$1_$2', 'camelCase'));
// prints camel_case
Answer from icc97 on Stack Overflow
🌐
W3Schools
w3schools.com › php › func_regex_preg_replace.asp
PHP preg_replace() Function
In the returned string, instances of \n and $n will be replaced with the substring that was matched by the group or, if \0 or $0 are used, by the whole expression. Note: For each input string, the function evaluates the patterns in the order that they are given. The result of evaluating the first pattern on the string is used as the input string for the second pattern and so on. This can lead to unexpected behaviour. preg_replace(patterns, replacements, input, limit, count)
Discussions

regex - python preg_replace translate message to gsm formatted message - Stack Overflow
i am trying to to make a replace in order to send a gsm message later. i use the function below where it takes the message as parameter and convert it to a gsm formatted message. import re @ More on stackoverflow.com
🌐 stackoverflow.com
December 9, 2016
Regex from Python in PHP - Stack Overflow
I've got the script in Python data = re.sub("[\xF3].[\x00]","\n",data) How to use that regex in PHP? $data = preg_replace("[\xF3].[\x00]", "\n", $data); I tried it but doesn't work. More on stackoverflow.com
🌐 stackoverflow.com
python .replace() regex - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
preg_replace - PHP Coding Help - PHP Freaks
Firstly, 'hi'. Im a noob when it comes to php, ive been able to tweak stuff buths about as far as if goes. Heres what im wanting to do.. I wanting to replace any spaces in the url with '+'. The file name will produce something like 'a random file' More on forums.phpfreaks.com
🌐 forums.phpfreaks.com
October 31, 2008
🌐
Liferea
lzone.de › examples › PHP preg_replace
LZone
LZone - Cheat Sheets for Sysadmin / DevOps / System Architecture
🌐
Python
mail.python.org › pipermail › python-list › 2002-April › 137417.html
preg_replace?
April 17, 2002 - In PHP > somebody helped me with the following solution: > > $s = preg_replace("%\[[^\]]*\]%", "0", $s); > > Even though I've been reading through regex-tutorials in the meantime, I'm > still not familiar with it. But is there a simple rule how to convert the > above into Python code?
🌐
Stack Overflow
stackoverflow.com › questions › 41061040 › python-preg-replace-translate-message-to-gsm-formatted-message
regex - python preg_replace translate message to gsm formatted message - Stack Overflow
December 9, 2016 - i am trying to to make a replace in order to send a gsm message later. i use the function below where it takes the message as parameter and convert it to a gsm formatted message. import re @
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python string tutorial › python regex replace
Python regex replace | Learn the Parameters of Python regex replace
May 13, 2024 - In this article, we saw what a regular expression is and why are they used when we want to replace any part of a string with a specified pattern. In Python, we use replace() function in the string concept, and it cannot be used for replacing the substring, or part of string were to replace() function is used to replace the entire string; hence to do this, we use regular expression which provides “re” module and to replace part of the string we use sub() function as we saw how simple it is to use this function for replacing the given pattern in the actual string to obtain the modified string.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 40291192
Regex from Python in PHP - Stack Overflow
I've got the script in Python · Copydata = re.sub("[\xF3].[\x00]","\n",data) How to use that regex in PHP? Copy$data = preg_replace("[\xF3].[\x00]", "\n", $data); I tried it but doesn't work. PHP Collective · php · python · regex · Share · Share a link to this question ·
🌐
PythonTest
pythontest.com › python › regex-search-replace
Python regex Search and Replace Examples | PythonTest
Search and replace is such a common task that it should be a tool that’s in every command line script author’s toolbox. There are probably endless solutions to the problem. I’ve put together my standard methods for tackling the problem.
🌐
Tutorialspoint
tutorialspoint.com › php › php_preg_replace.htm
PHP - Function preg_replace()
Python TechnologiesDatabasesComputer ... preg_replace() function operates just like POSIX function ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters....
🌐
Activestate
code.activestate.com › lists › python-list › 273693
preg_replace? « python-list « ActiveState List Archives
April 17, 2002 - Community · Downloads ▼ · ActivePerl · ActivePython · ActiveTcl · ActiveGo · Komodo IDE · more ▼ · Lists · PyPM Index
🌐
PHP
php.net › manual › en › function.preg-replace.php
PHP: preg_replace - Manual
When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \1 notation for your backreference. \11, for example, would confuse preg_replace() since ...
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
preg_replace - PHP Coding Help - PHP Freaks
October 31, 2008 - Firstly, 'hi'. Im a noob when it comes to php, ive been able to tweak stuff buths about as far as if goes. Heres what im wanting to do.. I wanting to replace any spaces in the url with '+'. The file name will produce something like 'a random file'
🌐
Regex101
regex101.com › r › 6fuTyb › 1
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-preg_replace-function
PHP | preg_replace() Function - GeeksforGeeks
July 11, 2025 - The preg_replace() function is an inbuilt function in PHP that is used to perform a regular expression for search and replace the content.
🌐
Hydrocul
hydrocul.github.io › wiki › programming_languages_diff › string › replace.html
文字列を全置換するには (replace) | hydroculのメモ
改行を文字列で表現するには "" で囲んで \n とする必要があるが、正規表現では、'\n' という文字列が文字列リテラルではなく正規表現解釈の段階で改めて改行と認識される。なので preg_replaceの1つ目の引数は '' でもよいが ...
🌐
Reddit
reddit.com › r/phphelp › preg_replace
r/PHPhelp on Reddit: preg_replace
February 4, 2022 -

I have working perl code that i need to implement in PHP. I'm looking for a PHP way to solve the problem instead of a translation.

I have a large array (array_push) of strings (some as json, some as html, some as forms), I have a set of strings to act as transform on each row. In Perl it's like,,,

<pre> foreach (@set){ foreach $k (keys %transform) { if (m/$k/) { $repl = $transform{$k}; s/$k/$repl/; } } } </pre>

for each element in a set execute a loop of looking for a pattern from an associative array/hash, if the key matches in the element, replace the matched key string with the string value from the hash.

so for each line in the set every pattern from the transform array is checked against the line; is there a better way to do this? i was thinking to use preg_replace_callback to match and replace with a lookup. Is there a way to cache the pattern match data. (otherwise every pattern in the transform (25?), is reprocessed for every line.

🌐
Codedamn
codedamn.com › news › python
Python replace regex for searching and replacing strings
July 1, 2023 - To use regex in Python, we first need to import the re module. This can be done using the following line of code: ... One common use of regex is to find and replace certain parts of a string. This is accomplished using the re.sub() function in Python's re module.