im kinda strumped in a situation where i need to match a whole string with a regular expression rather than finding if the pattern exists in the string.
suppose if i have a regular expression
/\\^^\\w+\\$^/
what i want is that the code will run through various strings , compare the strings with the regular expression and perform some task if the strings start and end with a ^.
Examples
^hello world^ is a match
my ^hello world^ should not be a match
the php function preg_match matches both of the results
any clues ???
Anchor the ends.
/^...$/
Here is a way to do the job:
$strs = array('^hello world^', 'my ^hello world^');
foreach($strs as $str) {
echo $str, preg_match('/^\^.*\^$/', $str) ? "\tmatch\n" : "\tdoesn't match\n";
}
Output:
^hello world^ match
my ^hello world^ doesn't match
Actually, ^\^\w+\^$ will not match "^hello world^" because you have two words there; the regex is only looking for a single word enclosed by "^"s.
What you are looking for is: ^\^.*\^$
This will match "^^", "^hello world^", "^a very long string of characters^", etc. while not matching "hello ^world^".
You can use the regex:
^\^[\w\s]+\^$
^ is a regex meta-character which is used as start anchor. To match a literal ^ you need to escape it as \^.
So we have:
^ : Start anchor
\^: A literal ^
[\w\s]+ : space separated words.
\^: A literal ^
$ : End anchor.
Ideone Link
Another pattern is: ^\^[^\^]*\^$ if you want match "^hello world^" and not "hello ^world^" , while \^[^\^]*\^ if you want match "^hello world^" and world in the "hello ^world^" string.
For Will: ^\^.*\^$ this match also "^hello^wo^rld^" i think isn't correct.
Try
/^\^\s*(\w+\s*)+\^$/
Related
Could someone help with a preg_match expression I need it to match the - dash character at the start and end of a string. This is for tags e.g. match -my-tag- should then be my-tag so It only matches the start and end of a string and replace it the characters with and empty string
You can do that with this easy expression:
$string = "-my-tag-";
$tag = preg_replace("/^-(.*)-$/", "$1", $string);
^ and $ are used to match the start and the end of the string, while (.*) captures every other symbols.
You can read more about regular expressions in the official PHP Documentation.
I want to write a regex with assertions to extract the number 55 from string unknownstring/55.1, here is my regex
$str = 'unknownstring/55.1';
preg_match('/(?<=\/)\d+(?=\.1)$/', $str, $match);
so, basically I am trying to say give me the number that comes after slash, and is followed by a dot and number 1, and after that there are no characters. But it does not match the regex. I just tried to remove the $ sign from the end and it matched. But that condition is essential, as I need that to be the end of the string, because the unknownstring part can contain similar text, e.g. unknow/545.1nstring/55.1. Perhaps I can use preg_match_all, and take the last match, but I want understand why the first regex does not work, where is my mistake.
Thanks
Use anchor $ inside lookahead:
(?<=\/)\d+(?=\.1$)
RegEx Demo
You cannot use $ outside the positive lookahead because your number is NOT at the end of input and there is a \.1 following it.
I'm trying to get a string which matches by a regex pattern ( {$ ... } ). But I don't want the brackets and the $ sign returned.
For example
{$Testpath}/Testlink
should return
Testpath
My regex pattern looks like this at the moment:
^{\$.*}$
Try the following regex:
^\{\$\K[^}]*(?=\})
Regex101 Demo
This expression mathces start-of-string ^ then a literal { then a literal $ then it ignores those using \K anchor, then it matches one or more characters which aren't a } then it looks ahead (?=\}) for a literal }.
You may not need the end-of-line anchor $ because the text you are trying to match might not end at the end of the string and you may not need the start-of-line ^ anchor for the opposite reason, that is the pattern you are trying to match may not be at the start of the string or line.
I think you should remove ^ and $ and use the global modifier.
<?php
$string = 'user34567';
if(preg_match('/user(^[0-9]{1,8}+$)/', $string)){
echo 1;
}
?>
I want to check if the string have the word user follows by number that can be 8 symbols max.
You're very close actually:
if(preg_match('/^user[0-9]{1,8}$/', $string)){
The anchor for "must match at start of string" should be all the way in front, followed by the "user" literal; then you specify the character set [0-9] and multiplier {1,8}. Finally, you end off with the "must match at end of string" anchor.
A few comments on your original expression:
The ^ matches the start of a string, so writing it anywhere else inside this expression but the beginning will not yield the expected results
The + is a multiplier; {1,8} is one too, but only one multiplier can be used after an expression
Unless you're intending to use the numbers that you found in the expression, you don't need parentheses.
Btw, instead of [0-9] you could also use \d. It's an automatic character group that shortens the regular expression, though this particular one doesn't save all too many characters ;-)
By using ^ and $, you are only matching when the pattern is the only thing on the line. Is that what you want? If so, use the following:
preg_match( '/^user[0-9]{1,8}[^0-9]$/' , $string );
If you want to find this pattern anywhere in a line, I would try:
preg_match( '/user[0-9]{1,8}[^0-9]/' , $string );
As always, you should use a reference tool like RegexPal to do your regular expression testing in isolation.
You were close, here is your regex : /^user[0-9]{1,8}$/
try the following regex instead:
/^user([0-9]{1,8})$/
Use this regex:
/^user\d{1,8}$/
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 (.*)