I can't get this function working correctly:
function isValidURL($url){
return preg_match('%http://domain\.com/([A-Za-z0-9.-_]+)/([A-Za-z0-9.-_]+)%', $url);
}
The url:
http://domain.com/anything-12/anything-12/
can contain numbers, letters and symbols _ -
I assume its to do with the first regex - as these work
http://domain.com/anything12/anything12/
http://domain.com/anything12/anything-12/
http://domain.com/anything12/any-thing-12/
http://domain.com/anything_12/any-thing-12/
As always all help is appreciated and thanks in advance.
You need to escape the - in the character class of your regex.
You need to anchor your regex so that tries to match the entire input string and not part of it.
The modified regex is:
'%^http://domain\.com/([A-Za-z0-9.\-_]+)/([A-Za-z0-9.\-_]+)/$%'
You can shorten your regex by noting that [A-Za-z0-9_] is same as \w and also there is a repeating sub-regex.
'%^http://domain\.com(/[\w.-]+){2}/$%'
Related
I am trying to set-up a quite complex regexp, but I can't avoid just one element from not-match list.
My regular expression is:
1234567-8_abc((?!_ABC|_DEFGHI)[\w]?)*(\.ios|\.and)
What I have to exclude is:
1234567-8_abc.ios
1234567-8_abc_DEFGHI.ios
1234567-8_abc_ABC.ios
Instead, what I have to include is:
1234567-8_abc_1UP.ios
1234567-8_abc_FI.ios
1234567-8_abc_gmg.ios
1234567-8_abc_1UP.and
1234567-8_abc_FI.and
1234567-8_abc_gmg.and
1234567-8_abc_ddd.and
1234567-8_abc_qwert.ios
1234567-8_abc_88.ios
Well, I can't exclude the first option (1234567-8_abc.ios).
I tried it here.
How can I achieve this?
Thank you!
You can use this pattern:
1234567-8_abc_[^_.]++(?<!_ABC|_DEFGHI)\.(?:ios|and)
Note: I assume that each substring between _ and .ios doesn't contain a dot or an underscore.
The possessive quantifier ++ is necessary to fail faster with the less possible backtracking steps
This regex matches your examples in PHP:
1234567-8_abc_((?!ABC|DEFGHI)[\w]?)*(\.ios|\.and)
Add a negative lookahead like below,
1234567-8_abc(?!_ABC|_DEFGHI)\w+(\.ios|\.and)
DEMO
(?!_ABC|_DEFGHI) Negative lookahead asserts that the string following _abc wouldn't be _ABC or _DEFGHI . And it must have one or more word characters before .ios or .and. So it won't match this 1234567-8_abc.ios string.
1234567-8_abc(?:(?!_ABC|_DEFGHI)\w)+(\.ios|\.and)
Try this.Your regex has left \w after 1234567-8_abc optional.Just made it compulsary.See demo.
http://regex101.com/r/bB8jY7/1
sorry for my English.
I’m trying to use preg_match with utf-8 in PHP.
preg_match("/\bjaunā\b Iel.*/iu", "Jaunā Iela");
Function returns 0. But
preg_match("/\bjauna\b Iel.*/iu", "Jauna Iela");
works fine.
Why?
Thanks.
Word boundaries don't work correctly with special chars. In the text Jaunā Iela the word bounderies are: \bJaun\bā \bIela\b
So instead of using word bounderies, try a look-ahead and look-behind assertion for a space. (or beginning of string) Like so:
The regex:
(?<=^|\s)Jaunā(?=\s) Iel.*
PHP:
preg_match("/(?<=^|\s)Jaunā(?=\s) Iel.*/i", "Jaunā Iela");
Working regex example:
http://regex101.com/r/tV6yR9
I'm trying to match the value of query v in the following regex:
http:\/\/www\.domain\.com\/videos\/video.php\?.*v=([a-z0-9-_]+)
A sample url:
http://www.domain.com/videos/video.php?v=9Gu0sd2dmm91B9b1
The url is always www and I'm only trying to match the v value. Does anyone know what's wrong with my syntax?
Use the parse_url() function. It's way easier to use:
$url_components = parse_url("http://www.domain.com/videos/video.php?v=9Gu0sd2dmm91B9b1");
echo $url_components['query'];
From there I think you can do the rest and slice off the first couple of letters. Once you do that you're left with only the stuff after v=.
you forget the capital letters
http:\/\/www\.domain\.com\/videos\/video.php\?.*v=([a-zA-Z0-9-_]+)
You are not escaping the period '.' in video.php. I also use a different delimiter if I am escaping paths/URL's - like this:
preg_match( "#http://www\.domain\.code/videos/video\.php\?.*v=([^&]*)#", $url, $matches );
If the v= is in the middle of the query string,
v=([^&]*)
.. will match everything up to another & symbol, just in case characters other than alphas and _,- end up in there for some reason.
I only knows basic regex, so I am look for help here.
I need to match URL with this pattern:
/kb/This-is-possible-title-12345.html
The URL will always ends with -nnnnn.html. Currently I have this regex pattern:
'kb/[a-zA-Z_-]*(\d+)\.html'
however, this does not work if the portion contains numbers, such as
/kb/This-is-12345-possible-title-12345.html
This needs to be done with PHP preg_match function.
The following works for me: /kb/[\w_-]*-(\d+)\.html$.
At a quick glance what you have looks right but you need to escape the forward slash so change '/' to '\/'.
'/kb/[^/]*-\d{5}\.html'
This matches "/kb/" "any characters except '/'" "hyphen" "5 digits" ".html"
How can convert the below youtube urls
$url1 = http://www.youtube.com/watch?v=136pEZcb1Y0&feature=fvhl
$url2 = http://www.youtube.com/watch?feature=fvhl&v=136pEZcb1Y0
into
$url_embedded = http://www.youtube.com/v/136pEZcb1Y0
using Regular Expressions?
Here's an example solution:
PHP:
preg_replace('/.+(\?|&)v=([a-zA-Z0-9]+).*/', 'http://youtube.com/watch?v=$2', 'http://www.youtube.com/watch?v=136pEZcb1Y0&feature=fvhl');
Match:
^.+(\?|&)v=([a-zA-Z0-9]+).*$
Replace with:
http://youtube.com/watch?v=$2
Here's how it works: regex analyzer.
suicideducky's answer is fine, but you changed the requirements. Try
preg_match($url1, "/v=(\w+)/", $matches);
$url_embedded = "http://www.youtube.com/v/" . $matches[1];
In case the wrong version was still cached, I meant $matches[1]!
add the string "http://www.youtube.com/watch/"
to the result of applying the regex "v=(\w+)" to the url(s) should do the job.
\w specifies alphanumeric characters (a-z, A-Z, 0-9 and _) and will thus stop at the &
EDIT for updated question.
My approach seems a little hackish.
so get the result of applying the regex "v=(\w+)" and then apply the regex "(\w+)" to it.
Then prefix it with the string "http://www.youtube.com/v/".
so to sum up:
"http://www.youtube.com/v/" + ( result of "(\w+)" applies to the result of ( "v=(\w+)" applied to the origional url ) )
EDITED AGAIN this approach assumes you are using a regex function that matches a substring instead of the whole string
Also, MvanGeest's version is superior to mine.