I'm trying to explode a string like this:
([a:b:c:d:...])
I have a code that partially works
([^\(\[\]\):])+
but it's not ideal since I need to make sure the string found is within the ([ ]) tags. But whenever I add them to the regexp, it stops working (can't find any matches).
\(\[([^\(\[\]\):])+\]\)
What am I doing wrong?
I'm using this website to test them regular expressions
http://myregextester.com/index.php
Thank you in advance.
I would do it in two parts, first match the stuff between the brackets
\(\[([^)\]]*)\]\)
which will put the inner contents in to matches[1], then simply explode/split on :
Related
everyone, I'm looking for tips&tricks how to do something like regexp_replace in VS code. I have huge amount of .php files in my project. Where language text are stored in multidimensional array() as i.e.: $lang['admin']['configuration_updated'] and I need to change it all to function getLangText('admin','configuration_updated')
I tried with regex \$lang\[(.)*\]\[(.)*\] but it replace all text. How to replace just part of string?
I need regex for VS code, not PHP function.
Thanks, in advance.
Use a non greedy pattern to match the content inside brackets:
\$lang\[(.*?)\]\[(.*?)\]
Then replace with:
getLangText($1, $2)
Demo
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)
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.
If I have string like this "location":"Denton TX" (including the double quotes)
I'd like to get just Denton TX. How can I write regular expression for that thing?
I tried
function getInfo($info,$content){
echo 'getting INFO';
preg_match_all("\.".$info."\.:\"[^\"]*(.*?)\.,",$content,$matches,PREG_PATTERN_ORDER);
print_r($matches);
return $matches[0];
}
and I put 'location' into $info but it doesn't work.
Simple get every character between quotes. You was too close.
"\.".$info."\.:\"([^\"]*)\"
I'd go with this:
"\"".$info."\":\"([^\"]+)\""
I'm not sure why you wrote this particular bit, but the main problem in your regex is the [^\"]*, which is too greedy and will consume everything, and leave (.*?) empty (well, unless is starts with a double quote, I guess). I also don't really understand why you what a comma in the end, but maybe your example wasn't descrptive... In any case, the regex I provided should match your example, provided $info contains the string location.
Try this: (?<=:")(.*)(?=")
I use a similar one to remove element tags from an XML response in an Android app. Good luck
I am using a Regular Expression to perform a find and replace with dreamweaver. I am running into some difficulty. This is what I have in my page (note that there is a syntax error because I need an additional parenthesis at the end of the string).
$email=htmlspecialchars(mysql_real_escape_string($_POST['email']);
$name=htmlspecialchars(mysql_real_escape_string($_POST['name']);
I am trying to performa a find and replace that will produce this:
$email=htmlspecialchars(mysql_real_escape_string($_POST['email']));
$name=htmlspecialchars(mysql_real_escape_string($_POST['name']));
This is what I am using to perform the find. It seems to be replacing too much text (it starts with the $_POST from the $email variable, but continues all the way down to the $_POST for the $name variable)
Find: \$_POST['([^<]*)']
Replace: $_POST['$1'])
I end up with this:
$email=htmlspecialchars(mysql_real_escape_string($_POST['email']);
$name=htmlspecialchars(mysql_real_escape_string($_POST['name']));
As you can see, it only fixes the last instance (this is because the find function is selecting both lines from $_POST['email'] all the way to $_POST['name']). Any ideas on how to fix this? Thank you!
Add a question mark to make it non-greedy. Also, you need to escape the [ and ] characters that you want to match.
Find: \$_POST\['([^<]*?)'\]
Replace: $_POST['$1'])
Or, alternatively, user a ' character instead of a < character to match the value within the quotes:
Find: \$_POST\['([^']*)'\]
Replace: $_POST['$1'])