everyone, I'm looking for tips&tricks how to do something like regexp_replace in VS code. I have huge amount of .php files in my project. Where language text are stored in multidimensional array() as i.e.: $lang['admin']['configuration_updated'] and I need to change it all to function getLangText('admin','configuration_updated')
I tried with regex \$lang\[(.)*\]\[(.)*\] but it replace all text. How to replace just part of string?
I need regex for VS code, not PHP function.
Thanks, in advance.
Use a non greedy pattern to match the content inside brackets:
\$lang\[(.*?)\]\[(.*?)\]
Then replace with:
getLangText($1, $2)
Demo
Related
My string is: /var/www/domain.com/public_html/foo/bar/folder/another/..
I want to remove the root folder from this string, to get only public folder, because some servers have multiple websites inside.
My actual regex is: /^(.*?)(www|public_html|public|html)/s
My actual result is: /domain.com/public_html/foo/bar/folder/another/..
But i want to remove the last ocorrence, and get somethig like this: /foo/bar/folder/another/..
Thanks!
You have to use a greedy quantifier and to check if the alternative is enclosed between slashes using lookarounds:
/^.*(?<![^\/])(?:www|public(?:_html)?|html)(?![^\/])/
About the lookarounds: I use negative lookarounds with a negated character class to check if there is a slash or the limit of the string at the same time. This way you are sure that for instance html is a folder and not the part of another folder name.
I removed the s modifier that is useless. I removed the capture groups too since the goal is to replace all with an empty string.
The ? makes your expression non-greedy which is not actually what you want here. Try:
^(.*)(www|public_html|public|html)
which should keep going until the last match.
Demo: https://regex101.com/r/v5WbB3/1/
i got this:
<?php echo Mage::helper('checkout')->formatPrice($tax['amount_incl_tax'],true,true); ?>
and i have to change it in this:
<?php echo preg_replace('/(.*)([\d]{2})([^\d].*)?$/','$1<span class="cents">$2</span>$3',Mage::helper('checkout')->formatPrice($tax['amount_incl_tax'],true,true)) ?>
using regex.
How to escape $1 in replace?
EDIT
Ok this question isn't clear.
I'm using Dreamweaver to manipulate a php file.
I've got many strings that ouput a formatted price.
I need to add a span tag to style the cents smaller than the rest.
To do that i need to wrap each string in a preg_replace() function.
All this stings i need to modify are different but got "formatPrice" in it.
So with Dreamweaver i'm goin to modify all this strings using regex in Find&Replace.
The new string contains "$1" and create a conflict with $1 Dreamweaver backreference, so i need a way to escape it.
The two strings above are in order an example of what i got and what should be after find and replace.
I found a work around for this:
as php use \1 as well as $1 as backreference i used this setting in dramweaver Find&Replace:
Find:([^\s]*formatPrice.*\));?\s
Replace:preg_replace('/(.*)([\d]{2})([^\d].*)?$/','\1<span class="cents">\2</span>\3',$1)
So i don't need to escape $1 anymore.
But still should be interesting find out if there is a way to use $1 in replace statment not as backreference
I'm creating a text parser, that basically looks for the following:
{IF SOMETHING} then include this text {ENDIF SOMETHING}
I can find that by using the regex:
/{IF [A-Z]+}.*{ENDIF [A-Z]}/
But that wont help if there are nested conditions. So i was looking to do something more like:
/{IF ([A-Z]+)}.*{ENDIF $1}/
But that doesn't seem to work - is it possible?
You can use this other syntax too: \g{1} that is useful to avoid confusion with a backreference followed with a literal digit. This syntax allows to use relative references like this:\g{-1} (i.e. the last defined capture group on the left)
$1 is only used in a replacement string with preg_replace.
PHP Regex uses \1 instead of $1. For more information, refer to the PHP Manual on regex back references.
I am using PHP, and I have been trying to create a regular expression pattern to capture part of URL path, but to no avail.
The possible URL path could be any of these:
"product/zzz"
"yyyyyyyy/product/zzz"
"xxxxx/yyyyyyyy/product/zzz"
"xxxxx/yyyyyyyy/.../product/zzz" (... means other possible words)
what I need to capture is the part before "product".
for the first case, the result should be an empty string.
for the rest, they are "yyyyyyyy", "xxxxx/yyyyyyyy" and "xxxxx/yyyyyyyy/..."
Can anyone here give me hint? thanks!
PS.
It looks like the part I wanted is a repetition of same pattern "xxxx/". but I am not good at using group of regex.
Update:
I probably found a solution, by capturing pattern "xxx/" with zero or more repetitions: "([^/]+/)*"
so the full regex should be "(([^/]+/)*)product/([^/]+)"
#SERPRO: it passed the test in your "Live RegExp".
Hope it is helpful.
I would use parse_url():
$path = parse_url($url, PHP_URL_PATH);
// Deal with $path to figure out what's after '/product/'
This should work for you:
#(.*?)/?product.*\b#
You can see an example of result strings here:
http://xrg.es/#5awa10
This should do it:
^(.*[^/]|)/*product/[^/]+/*$
It will also allow an arbitrary number of slashes at the end of the path.
The part inside parentheses is your result.
I'm trying to explode a string like this:
([a:b:c:d:...])
I have a code that partially works
([^\(\[\]\):])+
but it's not ideal since I need to make sure the string found is within the ([ ]) tags. But whenever I add them to the regexp, it stops working (can't find any matches).
\(\[([^\(\[\]\):])+\]\)
What am I doing wrong?
I'm using this website to test them regular expressions
http://myregextester.com/index.php
Thank you in advance.
I would do it in two parts, first match the stuff between the brackets
\(\[([^)\]]*)\]\)
which will put the inner contents in to matches[1], then simply explode/split on :