I would like to use a regular expression that finds only functions that are empty in php files
For example
function name_not_important()
{
}
Regex can be function\s[^\(]+\([^)]*\)(\n)*{(\n)*}
From https://regex101.com/:
function matches the characters function literally (case sensitive) \s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Match a single character not present in the list below [^(]
matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy) ( matches the
character ( literally (case sensitive) ( matches the character (
literally (case sensitive) Match a single character not present in the
list below [^)]
matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) ) matches the
character ) literally (case sensitive) ) matches the character )
literally (case sensitive) 1st Capturing Group (\n)*
matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) A repeated capturing
group will only capture the last iteration. Put a capturing group
around the repeated group to capture all iterations or use a
non-capturing group instead if you're not interested in the data \n
matches a line-feed (newline) character (ASCII 10) { matches the
character { literally (case sensitive) 2nd Capturing Group (\n)*
matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) A repeated capturing
group will only capture the last iteration. Put a capturing group
around the repeated group to capture all iterations or use a
non-capturing group instead if you're not interested in the data \n
matches a line-feed (newline) character (ASCII 10) } matches the
character } literally (case sensitive) Global pattern flags g
modifier: global. All matches (don't return after first match) m
modifier: multi line. Causes ^ and $ to match the begin/end of each
line (not only begin/end of string)
Note: This regex assumes that indentation of braces are in alignment.
I have this string that I want to clean up using PHP and regex:
Name/__text,Password/__text,Profile/__text,Locale/__text,UserType/__text,Passwor
dUpdateDate/__text,Columns/0/Name/__text,Columns/0/Label/__text,Columns/0/Order/
__text,Columns/1/Name/__text,Columns/1/Label/__text,Columns/1/Order/__text,Colum
ns/2/Name/__text,Columns/2/Label/__text,Columns/2/Order/__text,Columns/3/Name/__
text,Columns/3/Label/__text,Columns/3/Order/__text,Columns/4/Name/__text,Columns
/4/Label/__text,Columns/4/Order/__text,Columns/5/Name/__text,Columns/5/Label/__t
ext,Columns/5/Order/__text,Columns/6/Name/__text,Columns/6/Label/__text,Columns/
6/Order/__text,Columns/7/Name/__text,Columns/7/Label/__text,Columns/7/Order/__te
xt,Columns/8/Name/__text,Columns/8/Label/__text,Columns/8/Order/__text,Columns/9
/Name/__text,Columns/9/Label/__text,Columns/9/Order/__text,Columns/10/Name/__tex
t,Columns/10/Label/__text,Columns/10/Order/__text,Columns/11/Name/__text,Columns
/11/Label/__text,Columns/11/Order/__text,Columns/12/Name/__text,Columns/12/Label
/__text,Columns/12/Order/__text,Columns/13/Name/__text,Columns/13/Label/__text,C
olumns/13/Order/__text,MailAddress/__text,Description/__text,Columns/14/Name/__t
ext,Columns/14/Label/__text,Columns/14/Order/__text,Columns/15/Name/__text,Colum
ns/15/Label/__text,Columns/15/Order/__text
I want it to be Password,Profile,Locale,UserType,PasswordUpdateDate,Name,Label,Order...
I'm removing the /text or /__text after the word, but there are only sometimes things like Columns/0/ before the word to remove.
I tried this (below) regular expression in the regex tester, but it misses the first few items that don't have the Columns/2/ type of thing before it. I can't use a regex that will grab what's before /__text, because the / before the word is optional, like for the first Name. Any ideas how to do this? It's tough to search for this pattern or info on how to create it. Any help would be great!
[A-Za-z\/0-9]+\/([A-Za-z]+)\/[__text]
Probably easier to just match what you want and then join them on commas. Match a word (\w+) followed by \__text:
preg_match_all('#(\w+)/__text#', $string, $matches);
$result = implode(',', $matches[1]);
You could also use ([A-Za-z0-9]+) and add anything else instead of (\w+) in case it could be First_Name, First-Name, Firstname0 etc...
Regex:
(\w+)\/__text(?:(,)(?:Columns\/\d+\/)*)*
Demo
Explanation:
/(\w+)\/__text(?:(,)(?:Columns\/\d+\/)*)*/g
1st Capturing Group (\w+)
\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\/ matches the character / literally (case sensitive)
__text matches the characters __text literally (case sensitive)
Non-capturing group (?:(,)(?:Columns\/\d+\/)*)*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group (,)
, matches the character , literally (case sensitive)
Non-capturing group (?:Columns\/\d+\/)*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Columns matches the characters Columns literally (case sensitive)
\/ matches the character / literally (case sensitive)
\d+ matches a digit (equal to [0-9])
\/ matches the character / literally (case sensitive)
Sorry for a silly question,
but I have a variable (say $my_data) in which data is stored, where many times URL like href="http://PAGE_PATH/the_page_name" has been used.
I want that all href="http://PAGE_PATH/the_page_name" where the_page_name varies every single time gets replaced with onclick="jsonData('the_page_name', 'something')"
But I am stuck as the_page_name are different every time.
I think some str_replace like functions may be used? I don't know.
Help Appreciated!
EDIT
I have an example that I used previously but here the_page_name did not matter:
$base_paths = array("http://PAGE_PATH");
$web_paths = array(link_url()."page");
$content = str_replace($base_paths,$web_paths, $this->input->post('pg_content'));
I think a regex is your solution
a link here
Explanation
/href="https?:/{2}\S*/([^\r\n\t\f ]+)"/g
href="http matches the characters href="http literally (case sensitive)
s? matches the character s literally (case sensitive)
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
: matches the character : literally (case sensitive)
/{2} matches the character / literally (case sensitive)
{2} Quantifier — Matches exactly 2 times
\S* matches any non-whitespace character
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
/ matches the character / literally (case sensitive)
1st Capturing Group ([^\r\n\t\f ]+)
Match a single character not present in the list below [^\r\n\t\f ]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\ matches the character \ literally (case sensitive)
r matches the character r literally (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
\t matches a tab character (ASCII 9)
\f matches a form-feed character (ASCII 12)
** ** matches the character [space] literally (case sensitive)
" matches the character " literally (case sensitive)
<?php
$mydata = 'Sorry for a silly question, but I have a variable (say $my_data) in
which data is stored, where many times URL like
href="http://PAGE_PATH/the_page_name1" has been used.
I want that all href="http://PAGE_PATH/the_page_name2" where the_page_name2
varies every single time gets replaced with onclick="jsonData(\'the_page_name\', \'something\')"
href="http://PAGE_PATH/the_diff_name"
href="http://PAGE_PATH/the_other_name"
But I am stuck as the_page_name href="http://PAGE_PATH/another_name" are different every time.
I think some str_replace like functions may be used? I dont know.
Help Appreciated!';
$pattern = '/href="https?\:\/{2}\S*\/([^\\r\n\t\f ]+)"/i';
$replacement ='onclick="jsonData(\'\1\',\'something\')"';
echo "BEFORE<br/>$mydata<hr/>AFTER<br/>";
echo preg_replace($pattern, $replacement, $mydata );
?>
So basically I got links like these
https://dog.example.com/randomgenerated45443444444444
https://turtle.example.com/randomgenerated45443
https://mice.example.com/randomgenerated452
https://monkey.example.com/randomgenerated43232323
https://leopard.example.com/randomgenerated22222222222222222
I was wondering if it was possible to detect the words between https:// and .example.com/ which would be the random animal name. And replace it with "thumbnail". The amount of letters in the animal names and randomgenerated ones always vary in amount of letters in them
You can use a positive lookahead to get to the data you want:
$string = 'https://leopard.example.com/randomgenerated22222222222222222';
$pattern = '/(?=.*\/\/)(.*?)(?=\.)/';
$replacement = 'thumbnail';
$foo = preg_replace($pattern, $replacement, $string);
$protocol = 'https://';
echo $protocol . $foo;
returns
https://thumbnail.example.com/randomgenerated22222222222222222
Explanation of the regex:
Positive Lookahead (?=.*\/\/)
Assert that the Regex below matches
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\/ matches the character / literally (case sensitive)
\/ matches the character / literally (case sensitive)
1st Capturing Group (.*?)
.*? matches any character (except for line terminators)
*? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
Positive Lookahead (?=\.)
Assert that the Regex below matches
\. matches the character . literally (case sensitive)
Assuming that https:// and example.com never change, then this is the simplest regex you can use for the purpose:
https://(.+)\.example\.com
Anything in the (.+) will be the words you are attempting to extract.
Edit on 2016.10.27:
While the / character has no special meaning in Regular Expressions, it will likely need to be escaped (\/) if you are also using it as your expression delimiter. So the above will look like:
https:\/\/(.+)\.example\.com
I am trying to write an regualr expression to match invalid url patterns
I want to match following pattern :
/article/test-string/
Above is invalid url, but following are valid
/article/abc/test-string/ and /article/xyz/abc/test-string/
I want to match those which have only one value after article slash.
Please help, I am trying using following, but it is matching all :
/article/(.*)/$
.* matches 0 or more of any character so /article/(.*)/$ will match all the URIs that have /article/ in it.
You can use this regex to validate only only one non-slash component after /article/:
$re = '~^/article/[^/]*/$~';
[^/]* # matches 0 or more of any character that is not /
/$ # matches / in the end
~ is used as regex delimiter to avoid escaping /
~^/article/(.*)+/(.*)/$~gm
^ assert position at start of a line
/article/ matches the characters /article/ literally (case sensitive)
1st Capturing group (.*)+
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
/ matches the character / literally
2nd Capturing group (.*)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
/ matches the character / literally
$ assert position at end of a line
g modifier: global. All matches (don't return on first match)
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
$re = "~^/article/(.*)+/(.*)/$~m";
$str = "/article/xyz/abc/test-string/\n/article/test-string/";
preg_match_all($re, $str, $matches);
source https://regex101.com/