Case-sensitive regex that works - php

I have tried to change this regular expression to case-sensitive with a lot of possible solutions (/[u=|&l=|&dl=|&f=]/i and so on) but I didn't make it to work as I want to.
u=, &l=, &dl=, and &f= is taken from profile-photos.php?u=edgren&dl=. I use this regular expression to only get the username edgren and identify those other GETs (l, dl, and f) for example;
Looking at '.properize($profile).' '.(isset($_GET['l']) ? 'likes' : (isset($_GET['dl']) ? 'dislikes' : 'favorites')) which prints "Looking at edgrens dislikes" with the URL profile-photos.php?u=edgren&dl=.
The regular expression I have now, prints egren (example at regexpal.com) if the GET is &dl= which is wrong. I want to print the whole username and not the half of it, so to speak.
How can I fix my problem?
Thanks in advance.

You are confusing alternation with character classes. If you want to match one of several strings, use round brackets: (u=|&l=|&dl=|&f=). Square brackets are for character classes (which have the meaning "match one character if it is one of those specified between these square brackets").
Also i makes the regex explicitly case-insensitive.

Related

Simple Regex NOT on multidimensional JSON string

So i will provide this simple example of json string covering most of my actual string cases:
"time":1430702635,\"id\":\"45.33\",\"state\":2,"stamp":14.30702635,
And i'm trying to do a preg replace to the numbers from the string, to enclose them in quotes, except the numbers which index is already quoated, like in my string - '\state\':2
My regex so far is
preg_replace('/(?!(\\\"))(\:)([0-9\.]+)(\,)/', '$2"$3"$4',$string);
The rezulting string i'm tring to obtain in this case is having the "\state\" value unquoted, skipped by the regex, because it contains the \" ahead of :digit,
"time":"1430702635",\"id\":\"45.33\",\"state\":2,"stamp":"14.30702635",
Why is the '\state\' number replaced also ?
Tried on https://regex101.com/r/xI1zI4/1 also ..
New edit:
So from what I tried,
(?!\\")
is not working !!
If I'm allowed, I will leave this unanswered in case someone else does know why.
My solution was to use this regex, instead of NOT, I went for yes ..
$string2 = preg_replace('/(\w":)([0-9\.]+)(,)/', '$1"$2"$3',$string);
Thank you.
(?!\\") is a negative lookahead, which generally isn't useful at the very beginning of a regular expression. In your particular regex, it has no effect at all: the expression (?!(\\\"))(\:) means "empty string not followed by slash-quote, then a colon" which is equivalent to just trying to match a colon by itself.
I think what you were trying to accomplish is a negative lookbehind, which has a slightly different syntax in PCRE: (?<!\\"). Making this change seems to match what you want: https://regex101.com/r/xI1zI4/2

Regex expression to mach one of many strings in php

I am totally new to regex , I want to match if the value is any one of the following
cs,ch,es,ee,it,me
Till now I have tried
if (preg_match("/^[cs|ch|es|ee|it|me]{2}$/",$val))
echo "true";
else
echo "false";
Its working fine for true cases but also returns true for reverse of them like sc,hc etc.
Also it will be really helpful if you refer some good source/books to learn it for PHP.
Remove the character class [] from your regex and wrap them using (). Also remove the {2} as its not necessary anymore.
if (preg_match("/^(cs|ch|es|ee|it|me)$/",$val))
And this will do for you.
You need to use () insteadof []
/^(cs|ch|es|ee|it|me)$/
Note: While using parentheses do not use {2}
So your Final code is:
if (preg_match("/^(cs|ch|es|ee|it|me)$/",$val))
echo "true";
else
echo "false";
TO learn regex for php I will suggest this book its a good one for quick refere or refer this question for more.
You must use the grouping delimiters (parentheses). The character class delimiters (square brackets) are used for matching ranges of characters.
/^(cs|ch|es|ee|it|me)$/
If you only use the regular expressions to match something (and not capture anything) then you can use the (?:) grouping.
/^(?:cs|ch|es|ee|it|me)$/
One of the better websites for learning regular expressions is regular-expressions.info.
do you know what [] does ?
lets take an example [abcdef]
it will match any of the letters mentioned in the square brackets, suppose you are providing : ^[cs|ch|es|ee|it|me]{2}$
it will match a single character in the list cs|heitm
you can add a single letter howsoever times you want but it will match only once.
so it will match any word of two letters as you have mentioned starting with the letters cs|heitm
so it will match cs hs |s etc.
hope you understand it :)
the corrected regex should be
/^(cs|ch|es|ee|it|me)$/
this will match for exact literal words rather than letters.

PHP regular expression : match the closest one

I have a string like this
<div><span style="">toto</span> some character <span>toto2</span></div>
My regex:
/(<span .*>)(.*)(<\/span>)/
I used preg_match and it returns the entire string
<span style="">toto</span> some character <span>toto2</span>
I want it returns:
<span style="">toto</span>
and
<span>toto2</span>
What do I need to do to achieve this? Thanks.
How about this:
/(<span[^>]*>)(.*?)(<\/span>)/
Check the docs here at PHP preg_match Repetition:
By default, the quantifiers are "greedy", that is, they match as much as possible
and
However, if a quantifier is followed by a question mark, then it becomes lazy, and instead matches the minimum number of times possible
Even though I guess all previous answers are correct, I just want to add that as you only want to capture the whole expressions (i.e. from to ) you don't have to capture eveything inside the regexp with ()
The following does what you expect without capturing additional expressions
/(<span\w*[^>]*>[^<]*<\/span>)/
(tested on http://rubular.com/)
EDIT : of course there might be some differences between PHP and ruby regexp implementations, but the idea is the same :)

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

Match a regular expression against any non-character or number

Ok, here again.
I'll promise to study deeply the regular expression soon :P
Language: PhP
Problem:
Match if some badword exist inside a string and do something.
The word must be not included inside a "greater word". I mean if i'll search for "rob" (sorry Rob, i'm not thinking you're a badword), the word "problem have to pass without check.
I'd googled around but found nothing good for me. So, I thought something like this:
If i match the word with after and before any character of the following:
.
,
;
:
!
?
(
)
+
-
[whitespace]
I can simulate a check against single word inside a string.
Finally the Questions:
There's a better way to do it?
If not, which will be the correct regexp to consider [all_that_char]word[all_that_char]?
Thanks in advance to anyone would help!
Maybe this is a very stupid question but today is one of that day when move our neurons causes an incredible headache :|
Look up \b (word boundary):
Matches at the position between a word
character (anything matched by \w) and
a non-word character (anything matched
by [^\w] or \W) as well as at the
start and/or end of the string if the
first and/or last characters in the
string are word characters.
(http://www.regular-expressions.info/reference.html)
So: \brob\b matches rob, but not problem.
You can use \b, see Whole word bounderies.

Categories