Let's say I have the this text (not to be treated as PHP code):
$this->validation->set('username','username','trim');
$this->validation->set('password','password','trim');
$this->validation->set('password2','password2','trim');
$this->validation->set('name','name','trim');
$this->validation->set('surname','surname','trim');
I want to get the list of first words after set( which is in quotation marks in every line, so the output of previous input must be like this:
username
password
password2
name
surname
I think, it's possible with regular expressions. My question is how can I get the list of the words which is in first quotation marks with PHP?
Lets say the variable $text holds the data from your question.
Let's analyse the regular expression /set\('(.*?)'/:
/ is the delimiter.
set\(' and ' are the strings set(' and ', respectively.
.*? is the least amount of (arbitrary) characters between the two aforementioned strings.1
As a result, this regular expression matches:
$this->validation->set('username','username','trim');
To store all the strings you need in the array $matches[1], we can use the function preg_match_all.
It suffices to call preg_match_all("/set\('(.*?)'/", $text, $matches).
1 See also: Regex Tutorial - Repetition with Star and Plus - Laziness Instead of Greediness
Example code:
$text = <<<EOF
\$this->validation->set('username','username','trim');
\$this->validation->set('password','password','trim');
\$this->validation->set('password2','password2','trim');
\$this->validation->set('name','name','trim');
\$this->validation->set('surname','surname','trim');
EOF;
preg_match_all("/set\('(.*?)'/", $text, $matches);
print_r($matches[1]);
$arr = explode("'","this->validation->set('surname','surname','trim')");
print_r($arr);
not sure why you would want to do something like that, but the above should work
Related
I have a string that contains something like "LAB_FF, LAB_FF12" and I'm trying to use preg_replace to look for both patterns and replace them with different strings using a pattern match of;
/LAB_[0-9A-F]{2}|LAB_[0-9A-F]{4}/
So input would be
LAB_FF, LAB_FF12
and the output would need to be
DAB_FF, HAD_FF12
Problem is, for the second string, it interprets it as "LAB_FF" instead of "LAB_FF12" and so the output is
DAB_FF, DAB_FF
I've tried splitting the input line out using 2 different preg_match statements, the first looking for the {2} pattern and the second looking for the {4} pattern. This sort of works in that I can get the correct output into 2 separate strings but then can't combine the two strings to give the single amended output.
\b is word boundary. Meaning it will look at where the word ends and not only pattern match.
https://regex101.com/r/upY0gn/1
$pattern = "/\bLAB_[0-9A-F]{2}\b|\bLAB_[0-9A-F]{4}\b/";
Seeing the comment on the other answer about how to replace the string.
This is one way.
The pattern will create empty entries in the output array for each pattern that fails.
In this case one (the first).
Then it's just a matter of substr.
$re = '/(\bLAB_[0-9A-F]{2}\b)|(\bLAB_[0-9A-F]{4}\b)/';
$str = 'LAB_FF12';
preg_match($re, $str, $matches);
var_dump($matches);
$substitutes = ["", "DAB", "HAD"];
For($i=1; $i<count($matches); $i++){
If($matches[$i] != ""){
$result = $substitutes[$i] . substr($matches[$i],3);
Break;
}
}
Echo $result;
https://3v4l.org/gRvHv
You can specify exact amounts in one set of curly braces, e.g. `{2,4}.
Just tested this and seems to work:
/LAB_[0-9A-F]{2,4}/
LAB_FF, LAB_FFF, LAB_FFFF
EDIT: My mistake, that actually matches between 2 and 4. If you change the order of your selections it matches the first it comes to, e.g.
/LAB_([0-9A-F]{4}|[0-9A-F]{2})/
LAB_FF, LAB_FFFF
EDIT2: The following will match LAB_even_amount_of_characters:
/LAB_([0-9A-F]{2})+/
LAB_FF, LAB_FFFF, LAB_FFFFFF...
really struggling with this...hopefully someone can put me on the right path to a solution.
My input string is structured like this:
66-2141-A-AC107-7
I'm interested in extracting the string 'AC107' using a single regular expression. I know how to do this with other PHP string functions, but I have to do this with a regular expression.
What I need is to extract all data between the third and fourth hyphens. The structure of each section is not fixed (i.e, 66 may be 8798709 and 2141 may be 38). The presence of the number of hyphens is guaranteed (i.e., there will always be a total of four (4) hyphens).
Any help/guidance is greatly appreciated!
This will do what you need:
(?:[^-]*-){3}([^-]+)
Debuggex Demo
Explanation:
(?:[^-]*-) Look for zero or more non-hyphen characters followed by a hyphen
{3} Look for three of the blocks just described
([^-]+) Capture all the consecutive non-hyphen characters from that point forward (will automatically cut off before the next hyphen)
You can use it in PHP like this:
$str = '66-2141-A-AC107-7';
preg_match('/^(?:[^-]*-){3}([^-]+)/', $str, $matches);
echo $matches[1]; // prints AC107
This should look for anything followed by a hyphen 3 times and then in group 2 (the second set of parenthesis) it will have your value, followed by another hyphen and anything else.
/^(.*-){3}(.*)-(.*)/
You can access it by using $2. In php, it would be like this:
$string = '66-2141-A-AC107-7';
preg_match('/^(.*-){3}(.*)-(.*)/', $string, $matches);
$special_id = $matches[2];
print $special_id;
I'm attempting to match all text between {brackets}, however not if it is in quotation marks:
For example:
$str = 'value that I {want}, vs value "I do {NOT} want" '
my results should snatch "want", but omit "NOT". I've searched stackoverflow desperately for the regex that could perform this operation with no luck. I've seen answers that allow me to get the text between quotes but not outside quotes and in brackets. Is this even possible?
And if so how is it done?
So far this is what I have:
preg_match_all('/{([^}]*)}/', $str, $matches);
But unfortunately it only gets all text inside brackets, including {NOT}
It's quite tricky to get this done in one go. I even wanted to make it compatible with nested brackets so let's also use a recursive pattern :
("|').*?\1(*SKIP)(*FAIL)|\{(?:[^{}]|(?R))*\}
Ok, let's explain this mysterious regex :
("|') # match eiter a single quote or a double and put it in group 1
.*? # match anything ungreedy until ...
\1 # match what was matched in group 1
(*SKIP)(*FAIL) # make it skip this match since it's a quoted set of characters
| # or
\{(?:[^{}]|(?R))*\} # match a pair of brackets (even if they are nested)
Online demo
Some php code:
$input = <<<INP
value that I {want}, vs value "I do {NOT} want".
Let's make it {nested {this {time}}}
And yes, it's even "{bullet-{proof}}" :)
INP;
preg_match_all('~("|\').*?\1(*SKIP)(*FAIL)|\{(?:[^{}]|(?R))*\}~', $input, $m);
print_r($m[0]);
Sample output:
Array
(
[0] => {want}
[1] => {nested {this {time}}}
)
Personally I'd process this in two passes. The first to strip out everything in between double quotes, the second to pull out the text you want.
Something like this perhaps:
$str = 'value that I {want}, vs value "I do {NOT} want" ';
// Get rid of everything in between double quotes
$str = preg_replace("/\".*\"/U","",$str);
// Now I can safely grab any text between curly brackets
preg_match_all("/\{(.*)\}/U",$str,$matches);
Working example here: http://3v4l.org/SRnva
I have a string that contains variables in the format {namespace:name} I am trying to create a regular expression for finding all of the variables in the string. I have the following so far, but it isn't working:
$str = " {user:fname} and the last name = {user:lname}";
var_dump(preg_match_all("/^\{(\w):(\w)\}/", $str, $matches));
var_dump($matches);
But it isn't finding any of the tags. The variables can have any word for namespace and name, but letters only with no spaces. Any help would be appreciated.
Update
I tried the following also and received no results: "/\{(\w):(\w)\}/"
Remove the anchor ^ from the regex and allow variables with a length of more than one character.
/^\{(\w):(\w)\}/
becomes:
/\{(\w+):(\w+)\}/
is it possible to get a regular expression that do:
find first occurence of some word in a string
return a substring of a nr of letters before and after the occurence
but if it encounters a . (dot) before the nr of letters before and after occurence, it will just return the sub string before/after the dot.
return whole words
example:
"Anyone who knows how to do this. Create a program that inputs a regular expression and outputs strings that satisfy that regular expression. And bla bla"
if the keyword is 'program' and we put nr of letters to 20 it will return 20 letters before and after 'program'. But since it encounters a dot before it gets to 20 letters it will stop there.
"Create a program that inputs a regular..."
Is this possible with regexp? what php function do i have to use? is there any finnished script for this? I guess its a quite basic need when showing search results. Someone already got the function to share?
Here's Dav's regular expression in php:
<?php
$str = "Anyone who knows how to do this. Create a program that inputs a regular expression and outputs strings that satisfy that regular expression. And bla bla";
$key = "program";
$lim = 20;
$reg = "/([^.]{0,{$lim}})({$key})([^.]{0,{$lim}})/"; // /[^.]{0,20}program[^.]{0,20}/
$res = preg_match($reg, $str, $matches);
echo $matches[0];
print_r($matches); // $matches[1] is the pre-text, and $matches[3] is the post-text
The trickiest requirement is #4: "return whole words". One way you could handle this while still making use of the above regular expression is pull more text before and after than you really want (say 40 chars). Then you could preg_split the before and after text on whitespace, which would give you two arrays of words. Run the arrays through a function that gives you back a subset of the array where the total length of all the words is less than your limit of 20...
[^.]{0,MAXCHARS}wordtofind[^.]{0,MAXCHARS}
Replace MAXCHARS with the number corresponding to the maximum number of characters you want on each side.
The [^.] pattern matches any character that's not a period, and the {0,MAXCHARS} qualifier matches anywhere from 0 to MAXCHARS of those characters.