This is the code:
<?php
$eqn1="0.068683000000003x1+2.046124y1+-0.4153z1=0.486977512";
if(preg_match("/^[0-9]\.[0-9]{1,}x[0-9]$/",$eqn1,$vx1))
{
echo "X1 is:". $vx1[0];
echo "Match found.";
}
else
echo "Match not found.";
?>
OUTPUT:
Match not found.
Here, I'm trying to extract the value of x1 (that is,0.068683000000003) and storing it in the variable '$vx1'. It always returns, "Match not found.". What is wrong with my code? If you find any errors, please provide a solution.
Thanks.
Your regex works fine but you have to remove the ^ and $ char. You also have to put brackets around the group you want to capture.
if(preg_match("/([0-9]\.[0-9]*)x[0-9]$/",$eqn1,$vx1))
The chars ^ and $ mean to capture a line only containing the enclosed regex. But your line consist of more then just this number.
If you want to use them any way you have to expand the regex to something like this:
^.*="([0-9]\.[0-9]*)x[0-9].*$
See here http://regexr.com/3diqc for a working example.
This should solve the problem :
<?php
$eqn1="0.068683000000003x1+2.046124y1+-0.4153z1=0.486977512";
preg_match("/^[0-9]\.[0-9]{1,}/",$eqn1,$vx1);
print_r($vx1);
<?php
$eqn1="0.068683000000003x1+2.046124y1+-0.4153z1=0.486977512";
if(preg_match("/(?<vx1>^[0-9]\.[0-9]{1,})x[0-9]/", $eqn1, $vx1)) {
echo "X1 is:". $vx1['vx1']. "\n";
echo "Match found.";**strong text**
} else {
echo "Match not found.";
}
result
X1 is:0.068683000000003x1
Match found.array(3) {
[0]=>
string(19) "0.068683000000003x1"
["vx1"]=>
string(17) "0.068683000000003"
[1]=>
string(17) "0.068683000000003"
}
regular error : if(preg_match("/^[0-9].[0-9]{1,}x[0-9]$/",$eqn1,$vx1))
Related
I'm using this code inside PHP
case preg_match('/\/start( .*)?/', $text):
echo "got you";
break;
Using this regex all I need to do is catching following structure:
$text needs to be:
/start
or
/start xyz
Where "xyz" stands for random content. These are the two only formats which should be accepted by the regex. For some reason my regex seems to be not working as expected.
This should do the trick:
^\/start\s?[\S]*$
Here is an example in python DEMO:
import re
textlist = ["^/start xyz","/start","/start not to match"]
regex = "^/start\s?[\S]*$"
for text in textlist:
thematch = re.search(regex, text)
if thematch:
print ("match found")
else:
print ("no match sir!")
What it's doing: the line starts with /start and might have space, then there might be any amount of non space (including none) and then the line ends.
Hopefully that helps!
EDIT;
PHP version of this code.
$textlist = array("^/start xyz","/start","/start not to match");
$regex = "#^/start\s?[\S]*$#";
foreach($textlist as $text){
preg_match($regex, $text, $thematch);
if ($thematch){
print ("match found\n");
}else{
print ("no match sir!\n");
}
}
Demo here: https://3v4l.org/OFpnG
I'm trying to use a regex to find and replace all URLs in a forum system. This works but it also selects anything that is within bbcode. This shouldn't be happening.
My code is as follows:
<?php
function make_links_clickable($text){
return preg_replace('!(([^=](f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i', '$1', $text);
}
//$text = "https://www.mcgamerzone.com<br>http://www.mcgamerzone.com/help/support<br>Just text<br>http://www.google.com/<br><b>More text</b>";
$text = "#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa";
echo "<b>Unparsed text:</b><br>";
echo $text;
echo "<br><br>";
echo "<b>Parsed text:</b><br>";
echo make_links_clickable($text);
?>
All urls that occur in bb-code are following up on a = character, meaning that I don't want anything that starts with = to be selected.
I basically have that working but this results in selecting 1 extra character in in front of the string that should be selected.
I'm not very familiar with regex. The final output of my code is this:
<b>Unparsed text:</b><br>
#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa<br>
<br>
<b>Parsed text:</b><br>
#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa
You can match and skip [url=...] like this:
\[url=[^\]]*](*SKIP)(?!)|(((f|ht)tps?://)[-a-zA-Zа-яёЁА-Я()0-9#:%_+.\~#?&;/=]+)
See regex demo
That way, you will only match the URLs outside the [url=...] tag.
IDEONE demo:
function make_links_clickable($text){
return preg_replace('~\[url=[^\]]*](*SKIP)(?!)|(((f|ht)tps?://)[-a-zA-Zа-яёЁА-Я()0-9#:%_+.\~#?&;/=]+)~iu', '$1', $text);
}
$text = "#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa";
echo "<b>Parsed text:</b><br>";
echo make_links_clickable($text);
You can use a negative lookbehind (?<!=) instead of your negated class. It asserts that what is going to be matched isn't preceded by something.
Example
How to check if there is a tab at the end of a string? Which pattern works? I tried /\+t$/ but i get "There is NO tab at the end of the string" even though there is a tabulator at the end.
<?php
$text = "Come to the dark side. We have cookies ";
$pattern = "/\+t$/";
if(preg_match($pattern , $text))
{
echo "There is a tab at the end of the string";
}
else
{
echo "There is NO tab at the end of the string";
}
?>
Your regex /\+t$/ matches a + sign followed by character t at the end of a string.
I guess you want :
/\t$/ : a tabulation at the end of the string
or /\t+$/ : one or more tabulations at the end
I’m trying this, to get word in word boundaries with unicode characters:
if(preg_match("/(?<!\p{L})jaunā(?!\p{L}) Iel.*/iu", "Jaunā. Iela") > 0){
echo "<h1>Match!</h1>";
}
else{
echo "<h1>dont match</h1>";
}
Why I’m getting „Don’t match”?
I want to find word “jaunā” in all variations for example:
text ,jaunā text
text jaunā, text
text ,jaunā! text
jaunā text
jaunā, text
etc.
Thanks.
You can try with following regex:
/(?<!\p{L})jaunā(?!\p{L}).*? Iel.*/iu
After jaunā you have also . character, so with .*? you can match any other characters between jaunā and Iela
You regex isn't matching a dot after jaunā:
Try this:
if(preg_match("/(?<!\p{L})jaunā(?!\p{L})\. Iel.*/iu", "Jaunā. Iela") > 0) {
echo "<h1>Match!</h1>";
}
else{
echo "<h1>dont match</h1>";
}
I have a string:
8 nights you doodle poodle
I wish to retrieve every thing between nights and poodle, so in the above example, the output should be you doodle.
I'm using the below regex. Please can someone point out what I may be doing wrong?
if (preg_match("nights\s(.*)\spoodle", "8 nights you doodle poodle", $matches1)) {
echo $matches1[0]."<br />";
}
You're close, but you're accessing the wrong index on $matches1. $matches1[0] will return the string that matched in preg_match();
Try $matches1[1];
Also, you need to enclose your regex in / characters;
if (preg_match("/nights\s(.*)\spoodle/", "8 nights you doodle poodle", $matches1)) {
echo $matches1[1]."<br />";
}
Output
you doodle<br />
You probably want something like this
if (preg_match("/nights\s(.*)\spoodle/", "8 nights you doodle poodle", $matches1)) {
echo $matches1[1]."<br />";
}
Check out rubular.com to test your regular expressions. Here is another relevant question:
Using regex to match string between two strings while excluding strings