Problem
I want to select the first contents between two strings. For example:
https://subdomain.domain.com/lobby/het/login?retUrl=https://subdomain.domain.com/lobby/het/responsible?retUrl=https://uat-api.domain.com/forms/authorise-client?retUrl=https://subdomain.domain.com/lobby/het/login?retUrl=https://subdomain.domain.com/lobby/het/testing?retUrl=https://uat-api.domain.com/forms/authorise-client?locale=en-GB&client_id=123&response_type=token&redirect_uri=https://subdomain/rem/rep/sol.html&prompt=0&state=authorise-client
We can see in the above URL that the retUrl occurs many times.
Question
How can I select the contents of ONLY the first retUrl(bold in the above string) using a Regular Expression? So, we need the first string which begins with "retUrl=" and ends on the first occurrence of a ? after it. Is this even possible?
Tries which failed
(?=retUrl=)(.*\n?)(?=\?)
(retUrl=)(.*)$\?
This should work for you:
/retUrl=([^\?]*)/
With ([^\?]*) you can simply say get me everything until a question mark. So you can use the regex with preg_match() which will only give you the first match of that regex.
preg_match only finds the first match. So there you go.
By contrast, preg_match_all finds all matches.
Related
I'm having problems figuring out the right regex pattern for the search preg_match_all("THIS PART", $my_string). I need to find all hashtags in my string with the word after the hashtag included as well.
So, these strings should be found by the mentioned function:
Input
#hi im like typing text right here hihih #asdasdasdasd #
Result
#hi
#asasdasdasdasd
Input
#asd#asd xd so fun lol #lol
Result
#asd#asd2 would be two seperate matches and #lol would be matched aswell.
I hope the question made sense and thanks beforehand!
This should work:
/#(?<hash>[^\s#]+)/g
It searches for # and creates then a named group called hash, it stops matching after it reaches another # or after any whitespace character (\s).
You can use preg_match_all
preg_match_all('/(?<!\w)#\w+/', $your_string, $allMatches);
It will give all contain # tag word. hope it help you.
print_r($allMatches)
I have created a Regular Expression (using php) below; which must match ALL terms within the given string that contains only a-z0-9, ., _ and -.
My expression is: '~(?:\(|\s{0,},\s{0,})([a-z0-9._-]+)(?:\s{0,},\s{0,}|\))$~i'.
My target string is: ('word', word.2, a_word, another-word).
Expected terms in the results are: word.2, a_word, another-word.
I am currently getting: another-word.
My Goal
I am detecting a MySQL function from my target string, this works fine. I then want all of the fields from within that target string. It's for my own ORM.
I suppose there could be a situation where by further parenthesis are included inside this expression.
From what I can tell, you have a list of comma-separated terms and wish to find only the ones which satisfy [a-z0-9._\-]+. If so, this should be correct (it returns the correct results for your example at least):
'~(?<=[,(])\\s*([a-z0-9._-]+)\\s*(?=[,)])~i'
The main issues were:
$ at the end, which was anchoring the query to the end of the string
When matching all you continue from the end of the previous match - this means that if you match a comma/close parenthesis at the end of one match it's not there at match at the beginning of the next one. I've solved this with a lookbehind ((?<=...) and a lookahead ((?=...)
Your backslashes need to be double escaped since the first one may be stripped by PHP when parsing the string.
EDIT: Since you said in a comment that some of the terms may be strings that contain commas you will first want to run your input through this:
$input = preg_replace('~(\'([^\']+|(?<=\\\\)\')+\'|"([^"]+|(?<=\\\\)")+")~', '"STRING"', $input);
which should replace all strings with '"STRING"', which will work fine for matching the other regex.
Maybe using of regex is overkill. In this kind of text you can just remove parenthesis and explode string by comma.
I'm not sure where to start with this one but I'm hoping someone can help.
When a user submits a message I want to check the string and return the first letter of each sentence and place it into an array. Basically I want the first letter in the string and every letter immediately after a full stop (if there are any) placed in an array.
Has anyone ever done this in the past? Is it even possible?
Hoping you can help
Cj
Split the string in sentences - use explode() or preg_split() with the punctiation mark. Then loop all sentences an get the first letter using mb_substr($sentence, 0, 1); Don't forget to trim all senteces from whitespaces at the start or end of sentence.
You could use a regex like:
preg_match_all("/\b\w/", $yourString, $matches);
And then $matches will contain an array with an entry for every one of the characters your are looking for.
If I have a string that equals "firstpart".$unknown_var."secondpart", how can I delete everything between "firstpart" and "secondpart" (on a page that does not know the value of $unknown_var)?
Thanks.
Neel
substr_replace
start and length can be computed with strpos. Or you could go the regex route if you're comfortable learning about them.
As long as $unkonwn_var does not contain neither firstpart nor secondpart, you can match against
firstpart(.*)secondpart
and replace it with
firstpartsecondpart
You shoukd use a regexp to do so.
preg_replace('/firspart(.*)secondpart/','firstpartsecondpart',$yourstring);
will replace anything between the first occurence of firstpart and the last of secondpart, if you want to delete multiple time between first and second part you can make the expression ungreedy by replacing (.*) by (.*?) in the expression
preg_replace('/firspart(.*?)secondpart/','firstpartsecondpart',$yourstring);
I am searching a string for urls...and my preg_match is giving me an incorrect amount of matches for my demo string.
String:
Hey there, come check out my site at www.example.com
Function:
preg_match("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", $string, $links);
echo count($links);
The result comes out as 3.
Can anybody help me solve this? I'm new to REGEX.
$links is the array of sub matches:
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
The matches of the two groups plus the match of the full regular expression results in three array items.
Maybe you rather want all matches using preg_match_all.
If you use preg_match_pattern, (as Gumbo suggested), please note that if you run your regex against this string, it will both match the value of your anchor attribute "href" as well as the linked Text which in this case happens to comtain an url. This makes TWO matches.
It would be wise to run an array_unique on your resultset :)
In addition to the advice on how to use preg_match, I believe there is something seriously wrong with the regular expression you are using. You may want to trying something like this instead:
preg_match("_([a-zA-Z]+://)?([0-9a-zA-Z$-\_.+!*'(),]+\.)?([0-9a-zA-Z]+)+\.([a-zA-Z]+)_", $string, $links);
This should handle most cases (although it wouldn't work if there was a query string after the top-level domain). In the future, when writing regular expressions, I recommend the following web-sites to help: http://www.regular-expressions.info/ and especially http://regexpal.com/ for testing them as you're writing them.