What Regex for this? - php

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]*$

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

Regular expression to search in html template

I'm trying to find certain strings in a lot of files (cc. 2000 files).
What I basically need is to find any ID attribute in a html file except some certain ID's.
For example I want to find:
<a id="certain_id">sdfsdf</a>
But I want to exclude:
<a id="manage">Manage</a>
I only need a regular expression since I want to use it in Eclipse Search. But if I cannot I can make a php file to do it too.
Something like: id="(?!manage)" or similar. I don't want to replace anything I just want a list of elements in each file.
Thanks for the help
This worked for me:
id="(?!(manage|something)").*?"
^ ^ ^
| | Any character (not greedy), followed by a quote
| Negative lookahead to make sure there isn't manage|something and a quote
Match the id=" characters literally
Where manage and something are two IDs you don't want to match.
You could also use this to make it non-greedy:
id="(?!(manage|something)")[^"]+"
Regular Expressions doesn't contain a natural "not" operator.
I believe a workaround exists, for instance you could do something like: id="(?!(undesirable1|undesirable2|undesirable3))"
It's been a little while since I did anything with regex, but I think that should work.
Edit: I think nick's answer is better
http://txt2re.com/ Best Tool Ever

Beginner regexp case, simple match

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$/

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.

replace exact match in php

im new to regular expressions in php.
I have some data in which some of the values are stored as zero(0).What i want to do is to replace them with '-'. I dont know which value will get zero as my database table gets updated daily thats why i have to place that replace thing on all the data.
$r_val=preg_replace('/(0)/','-',$r_val);
The code im using is replacing all the zeroes that it finds for eg. it is even replacing zero from 104.67,giving the output 1-4.56 which is wrong. i want that data where value is exact zero that must be replaced by '-' not every zero that it encounter.
Can anyone please help!!
Example of the values that $r_val is having :-
10.31,
391.05,
113393,
15.31,
1000 etc.
This depends alot on how your data is formatted inside $r_val, but a good place to start would be to try:
$r_val = preg_replace('/(?<!\.)\b0\b(?!\.)/', '-', $r_val);
Where \b is a 0-length character representing the start or end of a 'word'.
Strange as it may sound, but the Perl regex documentation is actually really good for explaining the regex part of the preg_* functions, since Perl is where the functionality is actually implemented.
Again, it would be more than helpful if you could supply an example of what the $r_val string really looks like.
Note that \b matches at word boundaries, which would also turn a string like "0.75" into "-.75". Not a desirable result, I guess.
Whilst the other answer does work, it seems overly complex to me. I think you need only to use the ^ and $ chars either side of 0.
$r_val = preg_replace('/^0+$/', '&#45', $r_val);
^ indicates the regex should match from the beginning of the line.
$ indicates the regex should match to the end of the line.
+ means match this pattern 1 or more times
I altered the minus sign to it's html code equivalent too. Paranoid, yes, but we are dealing with numbers after all, so I though throwing a raw minus sign in there might not be the best idea.
Why not just do this?
if ( $r_val == 0 )
$r_val = '-';
You do not need to use a regex for this. In fact, I'd advise against doing so for performance reasons. The operation above is approximately 20x faster than the regex solution.
Also, the PHP manual advises against using regexes for simple replacements:
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().
http://us.php.net/manual/en/function.str-replace.php
Hope that helps!

Categories