I am trying to update hundreds of lines of comments in my php files. My editor allows me to use regular expressions to perform a search and replace. However, I don't know much about regular expression to write it. Please refer to example below.
Dump($Data1, 'Library_reports.php - Get_Filtered_InventoryReport() - $Data1');
Dump($Data2, 'Library_reports.php - Get_Filtered2InventoryReport() - $Data2');
Dump($Data3, 'Library_reports.php - GetFilteredInventoryReport() - $Data3');
to be replace with
Dump($Data1, __METHOD__.' - $Data1');
Dump($Data2, __METHOD__.' - $Data2');
Dump($Data3, __METHOD__.' - $Data3');
So basically, I want to search for
'Some_Alphanumeric_string()
and then replace it with a
__METHOD__.'
Give it a try: [A-Za-z0-9_]() it's nothing complicated here.
Edit:
[A-Za-z0-9_]+\(\)
StackOverflow eats my backslashes :)
Search with:
([a-zA-Z0-9]+)\(\)
Replace with:
^ intentionally left blank
Based on your description, this search regex will do the trick:
\b[a-z0-9_]+\b\(\)
...assuming you do case insensitive search. (It's an option in the Dreamweaver search/replace tool).
Otherwise:
\b[A-Za-z0-9_]+\b\(\)
Note: I've included the underscore in the character class based on your use of them in:
"Some_Alphanumeric_string()"
Related
I have to replace the following in my PHP code:
assert('is_array($myArray)');
assert('my_function_call($myVariable)');
to make it read like:
assert(is_array($myArray));
assert(my_function_call($myVariable));
The problem is that it occurs a lot of time in my code files, and I would have to open each and make the change.
I use NetBeans which has a find and replace functionality which uses regex. What regex to use for this?
Use find replace regex term:
assert\(\'is_array\($myArray\)\'\);
assert\(\'my_function_call\($myVariable\)\'\);
Escaping the regex characters fixes simple text terms.
Whats the best way to do a regex search and replace for all instances of array_key_exists() with the more efficient isset()? Please, no Donald Knuth quotes regarding optimizations and yes, I'm aware of the differences between the two functions.
This is what I'm currently using in my Netbeans search and replace:
search for:
array_key_exists\s*\(\s*'([^']*)'\s*,([^)]*)\)
replace with:
isset($2['$1'])
it works well , changing this:
array_key_exists('my_key',$my_array)
to
isset($my_array['my_key'])
but doesn't pick up instances like this:
array_key_exists($my_key,$my_array)
Not the most elegant solution, but adding to your current regex we find both types of search criteria.
array_key_exists\s*(\s*'|$['|\S]\s*,([^)]*))
The best I could do was to run a second search and replace to cover the instances that used variables for both arguments:
array_key_exists($my_key,$my_array)
search and replace 2:
search for:
array_key_exists\s*\(\s*(\$[^,]*)\s*,([^)]*)\)
replace with:
isset($2[$1])
If you need a WIDER spectrum when upgrading the PHP version rather than JUST this upper use case:
Didn't clean it up, but it should catch every instance I could think of.
Search:
array_key_exists\s*\(\s*([^,]*)\s*,\s*((\(\w+\))?[a-z0-9_$'"\{\}\[\]\-\>\:]*(\(\))*[a-z0-9$_\.\{\}\'\"\[\]\-\>\:]*)\)
Replace:
isset($2[$1])
I'm a bit out of my depth here, but I'm trying to let users write links, not entirely unlike how StackOverflow formats links.
Ideally, when a user enters [text to link here][23] in a text area, they will end up with text to link here when that information is written from the database to the page.
My part I'm failing is how to extract text to link and 23 from the matched pattern
This is the pattern I'm using, but a preg_replace using this will only allow me to replace the entire string, not operate on it. I might be mistaken in my use of preg_replace.
You can operate with preg_replace_callback, but even that is unnecessary:
preg_replace('/\[(.+?)\]\[(\d+)?\]/', '\1', $st);
This seems to be the trick you're looking for: ([[a-zA-Z0-9- ]+])([[0-9]+])
The parenthesis ( and ) are used for grouping...
I've got a problem with regexp function, preg_replace(), in PHP.
I want to get viewstate from html's input, but it doesn't work properly.
This code:
$viewstate = preg_replace('/^(.*)(<input\s+id="__VIEWSTATE"\s+type="hidden"\s+value=")(.*[^"])("\s+name="__VIEWSTATE">)(.*)$/u','^\${3}$',$html);
Returns this:
%0D%0A%0D%0A%3C%21DOCTYPE+html+PUBLIC+%22-%2F%2FW3C%2F%2FDTD+XHTML+1.0+Transitional%2F%2FEN%22+%22http%3A%2F%2Fwww.w3.org%2FTR%2Fxhtml1%2FDTD%2Fxhtml1-transitional.dtd%22%3E%0D%0A%0D%0A%3Chtml+xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxhtml%22+%3E%0D%0A%3Chead%3E%3Ctitle%3E%0D%0A%09Strava.cz%0D%0A%3C%2Ftitle%3E%3Clink+rel%3D%22shortcut+icon%22+href%3D%22..%2FGrafika%2Ffavicon.ico%22+type%3D%22image%2Fx-icon%22+%2F%3E%3Clink+rel%3D%22stylesheet%22+type%3D%22text%2Fcss%22+media%3D%22screen%22+href%3D%22..%2FStyly%2FZaklad.css%22+%2F%3E%0D%0A++++%3Cstyle+type%3D%22text%2Fcss%22%3E%0D%0A++++++++.style1%0D%0A++++++++%7B%0D%0A++++++++++++width%3A+47px%3B%0D%0A++++++++%7D%0D%0A++++++++.style2%0D%0A++++++++%7B%0D%0A++++++++++++width%3A+64px%3B%0D%0A++++++++%7D%0D%0A++++%3C%2Fstyle%3E%0D%0A%0D%0A%3Cscript+type%3D%22text%2Fjavascript%22%3E%0D%0A%0D%0A++var+_gaq+%3D+_gaq+%7C%7C+%5B%5D%3B%0D%0A++_gaq.push%28%5B
EDIT: Sorry, I left this question for a long time. Finally I used DOMDocument.
To be sure i'd split this match into two phases:
Find the relevant input element
Get the value
Because you cannot be certain what the attributes order in the element will be.
if(preg_match('/<input[^>]+name="__VIEWSTATE"[^>]*>/i', $input, $match))
$value = preg_replace('/.*value="([^"]*)".*/i', '$1', $match[0]);
And, of course, always consider DOM and DOMXpath over regex for parsing html/xml.
You should only capture when you're planning on using the data. So most () are obsolete in that regexp pattern. Not a cause for failure but I thought I'd mention it.
Instead of using [^"] to mark that you don't want that character you could use the non-greedy modifier - ?. This makes sure the pattern is matching as little as it can. Since you have name="__VIEWSTATE" following the value this should be safe.
Let's put this in practice and simplify the pattern some. This works as you want:
'/.*<input\s+id="__VIEWSTATE"\s+type="hidden"\s+value="(.+?)"\s+name="__VIEWSTATE">.*/'
I would strongly recommend checking out an alternative to regexp for DOM operations. This makes certain your code works also if the attributes changes order. Plus it's so much nicer to work with.
The main mistake was the use of funciton preg_replace, witch returns the subject - neither the matched pattern nor the replacement. Thank you for your ideas and for the recommendation of DOMDocument. m93a
http://www.php.net/manual/en/function.preg-replace.php#refsect1-function.preg-replace-returnvalues
The input: we get some plain text as input string and we have to highlighight all URLs there with <a href={url}>{url></a>.
For some time I've used regex taken from http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/, which I modified several times, but it's built for another issue - to check whether the whole input string is an URL or no.
So, what regex do you use in such issues?
UPD: it would be nice if answers were related to php :-[
Take a look at a couple of modules available on CPAN:
URI::Find
URI::Find::Schemeless
where the latter is a little more forgiving. The regular expressions are available in the source code (the latter's, for example).
For example:
#! /usr/bin/perl
use warnings;
use strict;
use URI::Find::Schemeless;
my $text = "http://stackoverflow.com/users/251311/zerkms is swell!\n";
URI::Find::Schemeless
->new(sub { qq[$_[0]] })
->find(\$text);
print $text;
Output:
http://stackoverflow.com/users/251311/zerkms is swell!
For Perl, I usually use one of the modules defining common regex, Regexp::Common::URI::*. You might find a good regexp for you in the sources of those modules.
http://search.cpan.org/search?query=Regexp%3A%3ACommon%3A%3AURI&mode=module