PHP regular expression without delimiters doesn't work [duplicate] - php

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).

Related

Regex: Expression selecting more than expected [duplicate]

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.

PHP isolate character surrounded by special character [duplicate]

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.

What is "~" in regex? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Tilde operator in Regular expressions
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
The code is from http://php.net/manual/en/functions.anonymous.php
I searched for what "~" is in regex and did not find an answer.
What does it do?
The first and last character of a regular expression in PHP (and other implementations) is known as the delimiter. Normally, you see a / being used, but in this case, someone chose ~. Read more here.
Not sure why ~ was chosen though; probably a habit of that particular developer. Normally, one chooses a different delimiter over / when the regular expression itself will contain slashes (e.g. matching URLs), so that slashes don't need to be escaped every time.
The symbol ~ is just used as delimiter in PHP regexps.

Php : regular expression to find a text between strings [duplicate]

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.

How to extract an id from facebook video link with regular expression? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regex to get value of URL parameter?
If you have a facebook link such as
http://www.facebook.com/photo.php?v=107084586333124
What regular expression would extract the number at the end 107084586333124
edit: I use php
Use PHP's built-in functions to reliably pull query string variables out of a URL. You would use something like:
parse_str(parse_url($url , PHP_URL_QUERY), $v);//where $v is not set yet or an empty array
$v = #$v['v'];//will now contain the value of v or be null if not found
I'm not familiar with the PHP regex engine, but this simple regular expression should do the trick. You will find your number in the first capture group.
v=(\d+)
Replace the following regexes with "".
.*?\?v\=
\D*.*
The remaining data is your number.

Categories