I am not very good in RegEx and saying that I am editing a website written in php. One of it's functions are failing because of this regex:
preg_match("/^minus\((\-?[\d\.]+)\)$/i",$val,$m)
I know it should be something like minus() but it doesn't seems to find any matches as I can't figure it out what needs to be inside the brackets.
The regex is meant to match something like this:
minus(0.12)
minus(-0.12)
minus(.12)
However it is inaccurate, since it would also match:
minus(0.1.2)
Correct would be:
/minus\(-?\d*(\.\d+)?\)/
You can test it here: https://regex101.com/r/zN5xI8/1
Related
So basically I'm trying to select all content that is in /thank-you/hello/, so this can be /thank-you/hello/x/, /thank-you/hello/y/, /thank-you/hello/z/, etc.
This is what I'm using right now:
preg_match ('#^/thank-you/hello/#', $_SERVER['REQUEST_URI']
This block of code only works for stuff that is in /thank-you/hello/.
How should I change this snippet to include all the other folders that are after /hello/?
I suggest you read more about regex
I also recommend regex101 to test and study the site
In the desired pattern you can replace the desired word from .*?
.: Matches any character other than newline (or including line terminators with the /s flag)
a*: Matches zero or more consecutive a characters.
a?: Matches an a character or nothing.
They may seem a little incomplete without their examples
I suggest you see their examples on regex101
example:
preg_match('#^/thank-you/hello/.*?/#', $_SERVER['REQUEST_URI']);
It may not be exactly what you want
Or something may increase or decrease later and you may want to make a change
I think everyone should learn regex so that they can implement what they want according to their own desires.
I do not think it is a good idea to use patterns that you do not know what they mean
I have the following regex:
/{{trans-template-id:(\d)+}}/i
And it matches {{trans-template-id:7}} perfectly.
However, I want to modify it so it will match {{trans-template-hash:asdf1234}} as well. How would I modify it?
I have gotten this far, but I don't know regex well enough it seems:
/{{trans-template-(id:(\d)+|hash:[a-zA-Z0-9]+)}}/i
If you want to make a common group, you can use
{{trans-template-(id|hash):((?<=id:)\d+|(?<=hash:)\w+)}}
Regex Demo
You can also use branch reset like
{{trans-template-(?|id:(\d+)|hash:(\w+))}}
Regex Demo
Need to get all URLs like:
http://domain.name/novostroyki/novyy_petergof/
http://domain.name/novostroyki/novyy_petergof/?var1=value1&val2=value2=...
but not the following ones:
http://domain.name/novostroyki/novyy_petergof/flats/
http://domain.name/novostroyki/novyy_petergof/flats/?var1=value1&val2=value2=...
Tried something like that, but it doesn't work as I wish:
/novostroyki/((?!flats)[a-z_0-9A-Z\.])*/?\??(.*)/
Try this regex:
/novostroyki/((?!flats)[\w.]*/?)*(\?.*)?
Not sure if it will be fine in all cases - it certainly should be in the ones listed above.
check if this suggestion is true:
if an url matches the regex on the list, it doesn't continue to the next item on the list.
then you can use:
the regex with all novostroyki after the regex with all novostroyki/flats/
Currently (in PHP) I have the following regex pattern:
\[(.*) (.*)=(.*)\]
This matches [doSomething limitation=true]
The end result being that my code will interpret that string and replace it with whatever value is coded to return for it.
However, some of my code needs multiple variables sent through to the function, for example:
[doSomething limitation=true otherlimitation=false sendfile=1 title="hello there"]
How can I make the (.)=(.) in the regex repeatable so that it matches every variable sent through including the first (most important) name of function?
The following may work for you:
\[(.*) ((.*)=(.*))+\]
You may also want to replace your asterisks with plus-signs. Currently, your regex would match [ =] as a valid string.
\[(.+) ((.+)=(.+))+\]
I have a problem with one regex expression to be used so i.e. the input string looks like
hello world and me or you
and I would like to match all from hello until the closest/nearest of the noisy words: and,or
so far I have come up with something like that:
preg_match_all("/^hello[A-Z0-9 -]*(or|and)/is",$string,$match);
but the problem is that it will return:
hello world and me or instead of hello world and since the or is first in
(or|and) list.
It would be really appreciated if anyone could tell me is there an option to tell regex engine to check which one is closer/nearer from the OR tokens list to match and used that one instead of checking the order as provided i.e. (or|and) in which case and should be used as its closer to initial pattern.
P.S.
changing an order inside (or|and) is not a solution as there are more words and you never know which one is nearer so it must be done on the algorithmic level.
many thanks for your advices.
The question mark after an asterisk (ie. /.*?/) tells the asterisked expression to be not greedy.
So your RegExp should be /^hello[A-Z0-9 -]*?(or|and)/is or something similar.
Use (capturing) subpatterns:
preg_match_all("/^(hello[A-Z0-9 -]*)(or|and)/is",$string,$match);
and $match[0][1], $match[1][1], $match[2][1] ... will contain the values as you need 'em.