I want to find URL like following with preg_match.
http://www.website.com/THE_ID_WHICH_I_WANT/RANDOM_CHARACTERS_AND_NUMBERS.RANDOM_SOMETHING.html
This is how far I got:
preg_match_all('%http://www.website\.com\/(\w+)%', $string, $matches);
But I also want that it to get the random characters.
Thank you.
For matching anything it's customary to use .+ or the non-greedy .*?
You might want to use \S+ which matches anything that isn't a space character. And even then it might be too much. But you didn't really elaborate about the context in which you want to use it.
preg_match_all('%http://www\.website\.com/(\w+)/(.*)\.html%', $string, $matches);
The above is assuming that you want to separate "THE_ID_WHICH_I_WANT" from the other random characters.
Example: http://regexr.com?2v9t7
Related
I have a Problem with RegEx and WhiteSpaces.
I want to split a Text in an Array which is marked with (....)
preg_match_all("/\([a-z0-9\s]+\)/i", $str,$a);
To catch whithe spaces I tried to use [\040] [\s] but nothing worked for me!
Is there a posibiltity to say [ANY Character DIGIT and WHITESPACES and special character] ?
greetz
fluxa
You can also said "any characters but not a )"
With your example :
preg_match_all("/\([^\)]+\)/i", $str, $a);
You could use a lazy search using.*? instead (which will stop matching as soon as it can, compared to greedy, .* which will match as much as possible).
Regex: \((.*?)\)
In code:
preg_match_all("#\((.*?)\)#", $str, $a);
get_by_my_column
If I only want to match the get_by portion of the above string, how can I do this? I keep reading on this regex cheatsheet that I should use \n but I can't figure out how to implement it properly...
I've tried variations of the following...
/((_){2})/
/(_+){2}/
/(\w+?_\w+?)_\w+/ (use non greedy quantifiers, your substring should be in capture group 1)
or just /\w+?_\w+?/ <---(edit: won't work, you do need that second underscore as regex structure to force the non greedy \w up to it :])
Do you need to use a regex for this? You could use explode() and just grab the first two elements of the resulting array.
Try
preg_match('/(^[a-z]+[_][a-z]+)/', $string, $results);
This matches a string that starts with a group of letters followed by an underscore followed by another set of letters.
Edit: (lowercase letters)
try /^get_by. ^ for the condition that g must be the starting character.
I have this text string:
$text="::tower_unit::7::/tower_unit::<br/>::tower_unit::8::/tower_unit::<br/>::tower_unit::9::/tower_unit::";
Now I want to get the value of 7,8, and 9
how to do that in preg_match_all ?
I've tried this:
$pattern="/::tower_unit::(.*)::\/tower_unit::/i";
preg_match($pattern,$text,$matches);
print_r($matches);
but it still all wrong...
You forgot to escape the slash in your pattern. Since your pattern includes slashes, it's easier to use a different regex delimiter, as suggested in the comments:
$pattern="#::tower_unit::(\d+)::/tower_unit::#";
preg_match_all($pattern,$text,$matches);
I also converted (.*) to (\d+), which is better if the token you're looking for will always be a number. Plus, you might want to lose the i modifier if the text is always lower cased.
Your regex is "greedy".
Use the following one
$pattern="#::tower_unit::(.*?)::/tower_unit::#i";
or
$pattern="#::tower_unit::(.*)::/tower_unit::#iU";
and, if you wish, \d+ instead of .*? or .*
the function should be preg_match_all
what is wrong with my preg_match ?
preg_match('numVar("XYZ-(.*)");',$var,$results);
I want to get all the CONTENT from here:
numVar("XYZ-CONTENT");
Thank you for any help!
I assume this is PHP? If so there are three problems with your code.
PHP's PCRE functions require that regular expressions be formatted with a delimiter. The usual delimiter is /, but you can use any matching pair you want.
You did not escape your parentheses in your regular expression, so you're not matching a ( character but creating a RE group.
You should use non-greedy matching in your RE. Otherwise a string like numVar("XYZ-CONTENT1");numVar("XYZ-CONTENT2"); will match both, and your "content" group will be CONTENT1");numVar("XYZ-CONTENT2.
Try this:
$var = 'numVar("XYZ-CONTENT");';
preg_match('/numVar\("XYZ-(.*?)"\);/',$var,$results);
var_dump($results);
Paste your example string into http://txt2re.com and look at the PHP result.
It will show that you need to escape characters that have special meaning to the regex engine (such as the parentheses).
You should escape some chars:
preg_match('numVar\("XYZ-(.*)"\);',$var,$results);
preg_match("/XYZ\-(.+)\b/", $string, $result);
print_r($result[0]); // full matches ie XYZ-CONTENT
print_r($result[1]); // matches in the first paren set (.*)
I am developing an application using PHP but I am new to regular expressions, I could not find a solution to my problem. I want to replace all occurences of #word with a link, i have written a preg_match for this:
$text=preg_replace('~#([\p{L}|\p{N}]+)~u', '#$1', $text);
The problem is, this regular expression also matches the html character codes like
'
and gives corrupt output. I need to exclude the words starting with &# but i do not know how to do that using regular expressions.
Thanks for your help.
'~(?<!&)#([\p{L}|\p{N}]+)~u'
That's a negative lookbehind assertion: http://www.php.net/manual/en/regexp.reference.assertions.php
Matches # only if not preceded by &
http://gskinner.com/RegExr/
use this online regular expression constructor. They have explanation for every flag you may want to use.. and you will see highlighted matches in example text.
and yes use [a-zA-Z]
You would need to add a [A-Za-z] rule in your regular expression statement so that it only limits itself to letters and no numbers.
I will edit with an example later on.