regex search for optional wildcard between 2 strings - php

I have seen similar problems but I cant quite get them to work for my scenario.
I am trying to use regex to find an optional wildcard between 2 strings.
Here is an example of the strings that the regex does find correctly: not happy
if the string was:
not very happy
The code must still match both cases.
The code I have is:
/\b(?<=not(*?).)happy\b/
(happy and very would be variables)
Any help pointing me in the right direction would be very much appreciated.

Some tweaking may be required, but this would match up to two words in between "not" and "happy":
/not(?:\s+\w+){0,2}\s+happy/

This ...
/not +([^ ]* +)?happy/
And feel free to replace ? with {0,2} (for example) to match up to 2 words between not and happy.

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 match numbers, but not HTML entities

Is there a regex to find all the digit sequences (\d+) in text, but not the ones forming HTML entities? Look like I should use both "look ahead" and "look behind" together, but I can’t figure out how.
For example, for the string ✑ #555 foo 777; I want to match only 555 and 777, but not 10001.
I’ve tried
~(?<!(&#)|\d])\d+(?![\d|;])~
But it seems to be too strict, as it returns no matches for cases like 777;
You can probably use this regex with lookarounds:
(?<!&#)\b\d+\b|(?:^|\b)\d+\b(?!;|$)
Demo: http://www.rubular.com/r/IUGqDf7Nfg
I’ve found the solution the next morning.
(?<![(&#)\d])\d+|\d+(?!\d|;)
It's quite big and poorly readable, but it works.
P.S. I think it’s a lot easier just do decode/hide the entities before processing and then put them back.

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

regex: find the part, which doesn't contain none of some words

how can i much the sentense, if it doesn't contain none of {word1,word2,word3}
where i must put ^ symbol?
i think it must looks like this
^([^word1|word2|word3])$
but it doesn't work.
could you help? thanks
Regex isn't the best tool for testing these sorts of conditions, but if you must then you can do it with negative lookaheads:
^(?!.*word1)(?!.*word2)(?!.*word3).*$
What you are trying to do won't work because [^...] is a negative character class with an unordered list of characters. What you wrote is equivalent to:
^([^123dorw|])$
Note also that depending on your needs you might also want to include word-boundaries in your regular expression:
^(?!.*\bword1\b)(?!.*\bword2\b)(?!.*\bword3\b).*$
im not familiar with the use of regex in htaccess, so my thoughts may be bit high level:
what you try looks like kind of:
if sentence contains not word1 | not word2 | not word3
then do something
i would suggest a solution the way:
if sentence contains word1|word2|word3
then do nothing
else do something
means don't use the negation in the "query", but in the result, which makes the regex simpler.

Categories