Match string with last part different - php

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

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 detect mentions but not detecting emails

I have the following code:
preg_match('/#([^# ]+)/', $image->caption->text, $matches)
and I wanted to basically detect mentions in a string. However the issue now is that it is confused with email address such that it detects email as a mention, so for example if I have aksdjasd#yahoo.com then this counts as a match. I guess what I want to say here is that before the # sign there should be a space. But how do I put that in to this regex?
EDIT:
I also wanted to detect #mentions at the beginning of the string as well
before the # sign there should be a space
You can use lookbehind (edited based on OP's comment below):
preg_match('/(?<= |^)#[^# ]+/', $image->caption->text, $matches);
Working Demo
My take:
preg_match('/(?<=\W|^)#(\w+)/', "#Easy? No.#anubhava try harder! #\t", $matches);
preg_match('/(?<=\W|^)#(\w+)/', "Easy? No.#anubhava try harder! #\t", $matches);
preg_match('/(?<=\W|^)#(\w+)/', "Easy? No.anubhava try harder! e#m #\t", $match);
Correctly recognizes #Easy , #anubhava and not the tab or email.
You can use look behinds to check behind your regex.
Try using: preg_match('/(?<![^\s])#([^#\s]+)/', $image->caption->text, $matches);
Important thing to note here, is that you also want to use \s in the class you check for AFTER the #. If not, you could end up matching #stuff<NEWLINE><nonmatch>. I did some quick testing and found the problems with just using space for a number of reasons. Here is a link to the tests.
This is also using a negative look behind, because if the mention is at the front of the string it would not match using a positive look behind. You have to account for blank in front of the positive matches.
I think just checking for a space alone could have risks. I think you might want to check for any whitespace character, just in case the mention is at the very beginning of the string.

URL routing regex

I'm trying to create a snippet of regex that will match a URL route.
Basically, if I have this route /users/:id I want /users/100 to match, but /users/100/edit not to match.
This is what I'm using now: users/(.*)/ but because of the greedy match it's matching regardless of what's after the user ID. I need some way of "breaking" the match if there's an /edit or something else on the end of the route.
I've looked into the Regex NOT operator but with no luck.
Any advice?
Are you just trying to collect digits?
You could use users/(\d*)/
And this one is how you would do it if you wanted to collect everything until a /, and it uses a NOT, ^/users/[^/]*$
You can use negative lookahead:
users/(.*)/(?!edit)
This will always require a trailing slash however. Maybe a better solution would be:
users/(\d+)(?!/edit)
See this post for more information.

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.

help with a regex code

i have this regex code
/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i
it works but there is a problem
you NEED http:// in the url for it to validate, and what i am making, the user will not want to add http:// to the url they want to just have example.com, if its possible i need it to work weather it has http:// or not
i don't know how to make my own regex, and ive searched but cannot find a one that does what i need, unless im just not looking in the right place. (Google :P)
Don't bother with regex. Use parse_url function.
You can just make it optional
/^((?:https?:\/\/+)?[\w\-]+\.[\w\-]+)/i
The (?:) around the part you don't want to have is a non capturing group, the ? afterwards makes it optional.
I'm not sure what the + after the second slash is good for, it says at least one of the preceding character. That means it allows also stuff like http://////////.
I hope you are aware, that this regex is far from matching valid URLs.
For example it will match stuff like
http://////////------------.-
or at least
http://N.O
^ after this position you can write what you want and it will match valid.
Here on Regexr you can see what your regex is matching.
See Purple Coder's answer for a probably better solution.
/^((https?:\/\/+)?[\w-]+.[\w-]+)/i
I'm using this :
// Validate that the string contains at least a dot .
var filterWebsite = /^([a-zA-Z0-9:_\.\-/])+\.([a-zA-Z0-9_\.\-/])+$/;

Categories