I've been using following regex to search the string containing "function(...){}":
/(?<=:)"function\((?:(?!}").)*}"/
The string might also contain "function ()" - space between function and bracket "()"
how can regex be defined to check "function()..." or " function ()..."?
Just add an optional space in the appropriate place:
/(?<=:)"function *\((?:(?!}").)*}"/
" *" matches zero or more spaces.
Try this:
/(?<=:)"function\s*\((?:(?!}").)*}"/
here \s* means zero or more ...occurrence of space...
(you are missing { to match?)
/(?<=:)"function[ ]*\((?:(?!}").)*}"/
This would do it.
Related
I need a regular expression to find all the lines that begins with /*
$num_queries = preg_match_all(
'REG_EXP',
file_get_contents(__DIR__ . DIR_PLANTILLAS . '/' . 'temp_template.sql')
);
I try this '^\/\*.*' but it does not work.
If you use this string: /^\/\*.*/ in the preg_match() function, it'll work. This pattern matches /* followed by maybe some text.
Make sure the regular expression will be performed on each line. I recommend that you first split the string (file contents) by a newline. You can use the function preg_split() in order to do so.
If you don't want to split the file contents by each line first, then you can use the following pattern: /(^|\n)\/\*(.*)/. That pattern matches first either the beginning of the string or a newline, followed by /*, followed by maybe some text.
Notice that in the patterns /^\/\*.*/ and /(^|\n)\/\*(.*)/ the / is used as delimiter. That means that further occurences of / must be escaped.
Please, note, what you deal with multiline content, but ^ means beginning of content, not a beginning of a line.
try (\/[^\r\n]+)[\r\n$]+
Try this.
^\/\*+[^\n]*$
Edit: correction re escaping the /
I don't know how to do the following:
find all instances of '/\s[2-9]\)/' and replace the space with a <br /> tag.
Something as simple as this doesn't work:
$preg_num = ' /\s[2-9]\)/';
$preg_fix = "'/<br>/[2-9]\)'";
preg_replace($preg_num,$preg_fix,$notes);
What do I need to change the $preg_fix variable to?
Using a lookahead is simpler than a backreference, IMHO.
$preg_num = '/\s(?=[2-9]\))/';
$preg_fix = '<br/>';
preg_replace($preg_num,$preg_fix,$notes);
This way, you are only replacing the space with the <br/>.
The replacement string is not treated as a regex — it is more like a literal string. If you are trying to put whatever digit [2-9] that was matched in the original regex into the replacement string, capture the character as a group and use a backreference.
$preg_num = '/\s([2-9])\)/';
$preg_fix = "<br />$1)'";
preg_replace($preg_num,$preg_fix,$notes);
For more information:
preg_replace's replacement parameter documentation
Regular expression grouping: "Use Round Brackets for Grouping"
I have some text like:
name: [my_name]
email: [my_email]
I'd like to grab the fields in square brackets with regex—how would I do that?
I've tried using this pattern: [*.?]
Unfortunately it doesn't work. PHP gives this error:
compilation failed: nothing to repeat at offset 0
What's wrong? Is the pattern correct?
The brackets are special characters in regex. To match them you'll have to escape them with a back-slash. Something like \[(.*?)\]. Adding the parens () captures whatever is matched inside it so you you can use it later. Otherwise you're just matching on the whole pattern and you'd have to manually strip the brackets.
You should move the * and escape the [ and ]. So make it \[.*\] Since . matches any character already and * says: 0 or more of that char. So .* is 0 or more of any char
No, you got the order wrong. It should be something like
\[(.*)\]
.* = Something repeated as many times as possible.
The compilation error you get is because the compiler does not now what to repeat, as [ is a special character in regular expressions. The ? you added would also allow nothing within the brackets, which I figured you don't want, so I removed it. The question mark makes the foregoing statement optional. The parentheses aroudn the .* are used to capture the result. If you don't add those, the regex will match, but you won't get whats inside the brackets as result.
<?php
$text =
"name: [my_name]
email: [my_email]";
$pattern = '/\[(.*)\]/';
$matches = array();
preg_match_all($pattern, $text, $matches);
$name = $matches[1][0];
$email = $matches[1][1];
print "$name<br />";
print "$email";
?>
will output
my_name
my_email
/ is the delimiter (not part of the actual pattern per se). The \ is for escaping the [ and ] brackets, as they define character class definitions in patterns when not escaped. ( and ) define subpatterns, which means that text captured by a subpattern will be put into the array referenced by the third parameter of preg_match_all (in this case $matches).
Escape it!
[ and ] special characters, so you need to escape them:
\[*.?\]
I am trying to make a function to remove all the bracket codes but it doesn't seem to be working,
function anti_code($content)
{
# find the matches and then remove them
$output = preg_replace("/\[a-z\s+\]/is", "", $content);
# return the result
return $output;
}
I want these codes to be removed in the output,
Agro[space]terrorism
Agro[en space]terrorism
so that I can get
Agroterrorism
I must be something wrong in my regular expression! Please let me know. Thanks.
You escaped the [], but didn't add a second set of unescaped [] to designate a character class. Also, the s is not necessary if you're not using the . metacharacter in your regex.
Try this:
/\[[a-z\s]+\]/i
If you don't care what's between the square brackets and just want to remove everything contained in them, this will do:
/\[[^]]+\]/i
Try \[[a-z\s]+\] It will capture brackets and all contents
I'm using PHP. I'm trying to get a Regex pattern to match everything between value=" and " i.e. Line 1 Line 2,...,to Line 4.
value="Line 1
Line 2
Line 3
Line 4"
I've tried /.*?/ but it doesn't seem to work.
I'd appreciate some help.
Thanks.
P.S. I'd just like to add, in response to some comments, that all strings between the first " and last " are acceptable. I'm just trying to find a way to get everything between the very first " and very last " even when there is a " in between. I hope this makes sense. Thanks.
Assuming the desired character is "double quote":
$pat = '/\"([^\"]*?)\"/'; // text between quotes excluding quotes
$value='"Line 1 Line 2 Line 3 Line 4"';
preg_match($pat, $value, $matches);
echo $matches[1]; // $matches[0] is string with the outer quotes
if you just want answer and not want specific regex,then you can use this:
<?php
$str='value="Line 1
Line 2
Line 3
Line 4"';
$need=explode("\"",$str);
var_dump($need[1]);
?>
/.*?/ has the effect to not match the new line characters. If you want to match them too, you need to use a regular expression like /([^"]*)/.
I agree with Josh K that a regular expression is not required in this case (especially if you know there will not be any apices apart the one to delimit the string). You could adopt the solution given by him as well.
If you must use regex:
if (preg_match('!"([^"]+)"!', $value, $m))
echo $m[1];
You need s pattern modifier. Something like: /value="(.*)"/s
I'm not a regex guru, but why not just explode it?
// Say $var contains this value="..." string
$arr = explode('value="');
$mid = explode('"', $arr[1]);
$fin = $mid[0]; // Contains what you're looking for.
The specification isn't clear, but you can try something like this:
/value="[^"]*"/
Explanation:
First, value=" is matched literally
Then, match [^"]*, i.e. anything but ", possibly spanning multiple lines
Lastly, match " literally
This does not allow " to appear between the "real" quotes, not even if it's escaped by e.g. preceding with a backslash.
The […] is a character class. Something like [aeiou] matches one of any of the lowercase vowels. [^…] is a negated character class. [^aeiou] matches one of anything but the lowercase vowels.
References
regular-expressions.info/Examples - Programming Language Constructs - Strings
Has variations on different string patterns (e.g. allowing escaped quotes)
Related questions
Difference between .*? and .* for regex
As much as is practical, negated character class is always a better option than .*?