Beginner regexp case, simple match - php

I want to create a pregmatch pattern which applies to:
http://site.local/app/**/admin
text. I created something, which looks good, but it also pass the
http://site.local/app/vf/adming
what I dont want to. The basically created pattern:
preg_match('/http:\/\/site.local\/app\/.*\\/admin/', $siteUrl)
how should it be corrected?
Btw: operators/admins, I created this thread previously and since then that account is disabled. https://stackoverflow.com/questions/11139579/i-need-a-regexp Now that you see, I really tried it hard, may I get that account back? If not, I understand

[a-zA-Z] only letters and {1,5} from 1 to 5 length. If you to allow numbers just change it to [a-zA-Z0-9]
$site = 'http://site.local/app/at/admin';
if(preg_match('/^http:\/\/site.local\/app\/[a-zA-Z]{1,5}\/admin$/', $site)){
echo 1;
}

Use ^ and $ to "tell" regex the start and end of your pattern.
preg_match('/^http:\/\/site.local\/app\/(.*)\/admin$/', 'http://site.local/app/abcd/admin');
preg_match('/^http:\/\/site.local\/app\/(.*)\/admin$/', 'http://site.local/app/abcd/admins');

I would say the problem is that .* matches all characters where you actually want to match two *.
/^http:\/\/site.local\/app\/[\*]{2}\\/admin$/
Should do it...
Edit: To exlpain myself to the person who marked down.
The asker said he wanted a preg_match to match the
text
http://site.local/app/**/admin
I did just that. How can you mark me down for understanding English?
But to statisfy the asker cos he did mean any chars and any number of chars between app and admin here is the amended version:
/^http:\/\/site.local\/app\/.*\\/admin$/

Related

(preg_match ('#^/thank-you/hello/#', $_SERVER['REQUEST_URI'])

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

REGEX at last one uppercase and one number

I searched everywhere but i couldn't find the right regex for my verificaiton
I have a $string, i want to make sure it contains at last one uppercase letter and one number. no other characters allowed just numbers and letter. is for a password require.
John8 = good
joHn8 = good
jo8hN = good
I will use preg_match function
The uppercase and letter can be everywhere in the word, not only at the begging or end
This should work, but is a bit of a mess. Consider using multiple checks for readability and maintainability...
preg_match('/^[A-Za-z0-9]*([A-Z][A-Za-z0-9]*\d|\d[A-Za-z0-9]*[A-Z])[A-Za-z0-9]*$/', $password);
Use lookahead:
preg_match('/^(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]+$/', $string);
Use this regex pattrn
^([A-Z]+([a-z0-9]+))$
Preg_match
preg_match('~^([A-Z]+([a-z0-9]+))$~',$str);
Demo
Your requisition need "precise syntax description", and a lot of examples for assert your description. Only 3 or 4 examples is not enough, is very open.
For last confirmed update:
preg_match('/^([a-z]*\d+[a-z]*[A-Z][a-z]*|[a-z]*[A-Z][a-z]*\d+[a-z]*)$/',$str)
History
first solution preg_match('/^[A-Z][a-z]+\d+$/',$str)
After your edit1: preg_match('/^[a-z]*[A-Z][a-z]*\d+$/',$str)
After your comment about utf8: hum... add at your question the valid language. Example: "José11" is a valid string?
After your edit2 ("jo8hN" is valid): and about number, can repeat? Well I suppose not. "8N" is valid? I suppose yes. preg_match('/^([a-z]*\d+[a-z]*[A-Z][a-z]*|[a-z]*[A-Z][a-z]*\d+[a-z]*)$/',$str) you can add more possibilities with "|" in this regex.

PHP Regex help - using nearer word from list as a boundry instead of provided in-list order

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.

Positive look ahead regex confusing

I'm building this regex with a positive look ahead in it. Basically it must select all text in the line up to last period that precedes a ":" and add a "|" to the end to delimit it. Some sample text below. I am testing this in gskinner and editpadpro which has full grep regex support apparently so if I could get the answers in that for I'd appreciate it.
The regex below works to a degree but I am unsure if it is correct. Also it falls down if the text contains brackets.
Finally I would like to add another ignore rule like the one that ignores but includes "Co." in the selection. This second ignore rule would ignore but include periods that have a single Capital letter before them. Sample text below too. Thanks for all the help.
^(?:[^|]+\|){3}(.*?)[^(?:Co)]\.(?=[^:]*?\:)
121| Ryan, T.N. |2001. |I like regex. But does it like me (2) 2: 615-631.
122| O' Toole, H.Y. |2004. |(Note on the regex). Pages 90-91 In: Ryan, A. & Toole, B.L. (Editors) Guide to the regex functionality in php. Timmy, Tommy& Stewie, Quohog. * Produced for Family Guy in Quohog.
I don't think I understand what you want to do. But this part [^(?:Co)] is definitely not correct.
With the square brackets you are creating a character class, because of the ^ it is a negated class. That means at this place you don't want to match one of those characters (?:Co), in other words it will match any other character than "?)(:Co".
Update:
I don't think its possible. How should I distinguish between L. Co. or something similar and the end of the sentence?
But I found another error in your regex. The last part (?=[^:]*?\:) should be (?=[^.]*?\:) if you want to match the last dot before the : with your expression it will match on the first dot.
See it here on Regexr
This seems to do what you want.
(.*\.)(?=[^:]*?:)
It quite simply matches all text up to the last full stop that occurs before the colon.

What Regex for this?

I'm trying to learn regular expression, because I can't do without them.
So, this is a list of different dimension patterns (for products to sale) :
40x30x75
46x38x23-27
Ø30H30
Ø25-18H27
So, what pattern to use to find each kind of dimensions ?
For example, now, I'm using this to find this kind of pattern 40x30x75, but it not works :
if(preg_match("#^[0-9][x][0-9][x][0-9]#", $dimension))
echo "ok"
Could you help me ?
Try the following regex:
(^[0-9]+x[0-9]+x[0-9]+$)|(^[0-9]+x[0-9]+x[0-9]+-[0-9]+$)|(^Ø[0-9]+H[0-9]+$)|(^Ø[0-9]+-[0-9]+H[0-9]+$)
So:
if (preg_match("/(^[0-9]+x[0-9]+x[0-9]+$)|(^[0-9]+x[0-9]+x[0-9]+-[0-9]+$)|(^Ø[0-9]+H[0-9]+$)|(^Ø[0-9]+-[0-9]+H[0-9]+$)/", $dimension))
echo "ok";
It probably can be simplified even more, maybe someone would want to have a go at that?
By the way, did you know about a website called RegExr it allows you to test your regular expessions, it has been very useful to me whenever I work with regex's.
Your regex is missing quantifiers, add a + sign behind the character classes in question to singal you're looking for one or more matches:
if(preg_match("#^[0-9]+x[0-9]+x[0-9]+#", $dimension))
echo "ok"
By default it's looking for one character of the class only. Single characters do not need the character class (albeit it was not wrong). See the x'es in the example above.
Your regex should be:
^[0-9]{2}x[0-9]{2}x[0-9]{2}$
[0-9] means a single character which is between 0 and 9. So, you either need to have two of those, or use a quantifier thing like {2}. Instead of [0-9] you could also use \d, meaning any digit. So, you could for example write:
^\d\dx\d\dx\d\d$
Tip: If you can't do without regular expressions, want to learn it and have an easier life, I can recommend you get RegexBuddy. Bought it for myself when I just got started, and it has helped me a lot.
This will validate the first two:
^[0-9]+x[0-9]+x[0-9]+-?[0-9]*$

Categories