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.
Related
There are names of records in which are mixed several types of SKU that may contains symbols, digits, etc.
Examples:
Name of product 67304-4200-52-21
67304-4200-52 Name of product
67304-4200 Name of product
38927/6437 Name of product
BKK1MBM06-02 Name of product
BKK1MBM06 Name of product
I need to preg_match (PHP) only SKU part with any symbols in any combinations.
So i wrote pattern:
/\d+\/\d+|\d+-?\d+-?\d+-?\d+|\bbkk.*\b/i
It works but not with [BKK*] SKU.
Is it way to combine all this types of SKU together in one pattern?
The pattern \d+-?\d+-?\d+-?\d+ means that there should be at least 4 digits as all the hyphens are optional, but in the example data the part with the numbers have at least a single hyphen, and consist of 2, 3 or 4 parts.
You could repeat the part with the digits and hyphen 1 or more times, and instead of using .*\b use \S*\b to match optional non whitespace chars that will backtrack until the last word boundary.
Note that if you use another delimiter in php than /, you don't have to escape \/
Using a case insensitive match:
\b(?:\d+(?:-\d+)+|bkk\S*|\d+\/\d+)\b
Explanation
\b A word boundary to prevent a partial word match
(?: Non capture group for the alternatives
\d+(?:-\d+)+ Match 1+ digits and repeat 1 or more times matching - and again 1+ digits (or use {1,3} instead of +)
| Or
bkk\S* Match bkk and optional non whitespace characters
| Or
\d+\/\d+ Match 1+ digits / and 1+ digits
) Close the non capture group
\b A word boundary
See a regex101 demo.
Use
\d+(?:\d+(?:-?\d+){3}|\/\d+)|\b[bB][kK][kK][A-Za-z0-9-]*
See regex proof.
REGEX101 EXPLANATION
1st Alternative \d+(?:\d+(?:-?\d+){3}|\/\d+)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group (?:\d+(?:-?\d+){3}|\/\d+)
1st Alternative \d+(?:-?\d+){3}
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group (?:-?\d+){3}
{3} matches the previous token exactly 3 times
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative \/\d+
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Alternative \b[bB][kK][kK][A-Za-z0-9-]*
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
Match a single character present in the list below [bB]
bB matches a single character in the list bB (case sensitive)
Match a single character present in the list below [kK]
kK matches a single character in the list kK (case sensitive)
Match a single character present in the list below [kK]
kK matches a single character in the list kK (case sensitive)
Match a single character present in the list below [A-Za-z0-9-]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
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 );
?>
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/
Can someone help me how to specific pattern for preg_match function?
Every word in string must end with dot
First character of string must be [a-zA-Z]
After each dot there can be a space
There can't be two spaces next to each other
Last character must be a dot (logicaly after word)
Examples:
"Ing" -> false
"Ing." -> true
".Ing." -> false
"Xx Yy." -> false
"XX. YY." -> true
"XX.YY." -> true
Can you help me please how to test the string? My pattern is
/^(([a-zA-Z]+)(?! ) \.)+\.$/
I know it's wrong, but i can't figure out it. Thanks
Check how this fits your needs.
/^(?:[A-Z]+\. ?)+$/i
^ matches start
(?: opens a non-capture group for repetition
[A-Z]+ with i flag matches one or more alphas (lower & upper)
\. ? matches a literal dot followed by an optional space
)+ all this once or more until $ end
Here's a demo at regex101
If you want to disallow space at the end, add negative lookbehind: /^(?:[A-Z]+\. ?)+$(?<! )/i
Try this:
$string = "Ing
Ing.
.Ing.
Xx Yy.
XX. YY.
XX.YY.";
if (preg_match('/^([A-Za-z]{1,}\.[ ]{0,})*/m', $string)) {
// Successful match
} else {
// Match attempt failed
}
Result:
The Regex in detail:
^ Assert position at the beginning of a line (at beginning of the string or after a line break character)
( Match the regular expression below and capture its match into backreference number 1
[A-Za-z] Match a single character present in the list below
A character in the range between “A” and “Z”
A character in the range between “a” and “z”
{1,} Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\. Match the character “.” literally
[ ] Match the character “ ”
{0,} Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)