This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I am using the following regex
'/\#(.*)\((.*)\)/'
And I am trying to get #ONE(TWO) one and two from the expression. Which works as long as it's the only time that it can be found before an end of line (I think)
I am quite green with regex and I really cannot understand what I am doing wrong.
What I need is to be able to get all the ONE/TWO couples. Can you please help me.
I am working with PHP and the following function
$parsed_string = preg_replace_callback(
// Placeholder for not previously created article
// Pattern example: #George Ioannidis(person)
'/\#(.*)\((.*)\)/',
function ($matches) {
return $this->parsePlaceholders( $matches );
},
$string
);
The results I am getting from https://regexr.com
* expression is greedy by default. For example such regexp (.*)a will return you bdeabde result on bdeabdea string. You should use special ? symbol for non-greedy * behavior. In your case try to use /\#(.*?)\((.*?)\)/ regexp.
Related
This question already has answers here:
Variable-length lookbehind-assertion alternatives for regular expressions
(5 answers)
Closed 4 years ago.
I cant get my regexpression to work in php. It works in javascript (vuejs):
(?<=.+: )(.*)
I have this string:
NL: abcdef
and i would like to get
abcdef
Can someone please tell me what i am doing wrong?
There are many ways to solve this using PHP/PCRE, one is to skip the preceding string using \K
[^:]+: \K(.*)
Regex Demo
If you can add an anchor to the beginning of the string, even better: ^[^:]+: \K(.*)
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
So i have variable
$var = "how-to-stack-overflow"
I want to ECHO "overflow", or the last word from the string upto a hyphen.
Please show any method to accomplish this, I know this question may have duplicates, but i am a newbie and can't figure out how to put regex expressions using PHP(many valid answers are just the regex without any php), Thanks a lot in advance :)
PLEASE ONLY SHOW USING NEWBIE PROOF PHP
/\w+$/
grab one or more word characters up to the end of the string.
Demo: https://regex101.com/r/onEfF8/1
Edit: Example in php
$var = "how-to-stack-overflow";
$regex = preg_match('/\w+$/', $var, $match);
$match = $match[0];
echo $match;
$var = "how-to-stack-overflow";
$explode=explode('-',$var);
$count=count($explode);
echo $explode[$count - 1];
This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 6 years ago.
I have the following string:
$db_string = '/var/www/html/1_wlan_probes.db';
I want to isolate/strip the number character so that I only have the following left:
$db_string = '1';
So far I havn't found an simply solution since the number that needs to be found is random and could be any positive number. I have tried strstr, substr and custom functions but none produce what I am looking after, or I'm simply overlooking somehthing really simple.
Thanks in advance
You should use the preg_match() function:
$db_string = '/var/www/html/1_wlan_probes.db';
preg_match('/html\/(\d+)/', $db_string, $matches);
print_r($matches[1]); // 1
html\/(\d+) - capture all the numbers that come right after the html/
You can test it out Here. It does not matter how long the number is, you're using a regular expression to match all of them.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
RegEx How to find text between two strings
I am new to REGEX and learning..this is emergency and some one need to help me,
how can i get a value 6Lf4 (dynamic value) ,
private="key" value="6Lf4" sent="yut"
P.s there are lots of attribitute named "Value" in an string, so i need some regex to find string between ="key" value=" , and "
This should work given a strict format:
$matches = array();
preg_match_all('/="key" value="([^"]*)"/', $inputString, $matches);
This tests specifically for the given format with no exceptions. If there is a variable amount of whitespace between "key" and value, you can change the regex to have key"\s+value instead.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
So I have some strings of this type:
choose_from_library_something
choose_from_library_something2
choose_from_library_something3
...
And I need to search for choose_from_library_*
here is my regular expression that doesn't work:
}elseif (preg_match('choose_from_library_.*',$form_name)) {
What I'm doing wrong?
You need to add delimiters to your regex:
preg_match('/^choose_from_library_.*/',$form_name)
EDIT: Added an anchor ^ to the beginning of the regex, to avoid matching don't_choose_from_library_, etc.
You may be better off using explode and count that off instead in your situation.
else if ( count ( explode("_" , $form_name) ) == 4 ) {
}
To answer your question, you need to have slashes around your pattern
/pattern/
also, asterisk means none or more, so even if you had no text, you it would still match it. The same would hold true for explode (but you can see if there was an empty string in the last element and return that as false).