What is wrong with my regex?

I need to get only these parts from the line below: Jony, Smith and example-free@wpdevelop.com

which, as you see, are between ^ and ~.

text^name1^Jony~text^secondname1^Smith~text^email1^example-free@wpdevelop.com~

for this i tried:

preg_match_all ('/\^(.*?)\~/', $row['form'], $res);

For the name it displays: ^ name1 ^ Jony ~ for the second name: ^ secondname1 ^ Smith ~ and email: ^ email1 ^ example-free@wpdevelop.com ~

As you can see, the word "text" has already disappeared, but not the name ^, name1, secondname1, email1 and ~

Can you tell me what is wrong in my regex?

-1
source share
3 answers

'/\^([^\^]*?)\~/', ., ^. ^ [^\^], ..

+1

.* [^^]*, " , ^"

<?php
$str = 'text^name1^Jony~text^secondname1^Smith~text^email1^example-free@wpdevelop.com~';

preg_match_all ('/\^([^^]*?)\~/', $str, $res);

var_dump($res);

/*
//output 

$ php scratch.php
array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(6) "^Jony~"
    [1]=>
    string(7) "^Smith~"
    [2]=>
    string(28) "^example-free@wpdevelop.com~"
  }
  [1]=>
  array(3) {
    [0]=>
    string(4) "Jony"
    [1]=>
    string(5) "Smith"
    [2]=>
    string(26) "example-free@wpdevelop.com"
  }
}
*/
+3

This is better:

<?php

$string = 'text^name1^Jony~text^secondname1^Smith~text^email1^example-free@wpdevelop.com~';

preg_match_all('/\^.+?\^(.+?)~/', $string, $matches);

var_dump($matches);

The result in $ match will be:

array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(12) "^name1^Jony~"
    [1]=>
    string(19) "^secondname1^Smith~"
    [2]=>
    string(35) "^email1^example-free@wpdevelop.com~"
  }
  [1]=>
  array(3) {
    [0]=>
    string(4) "Jony"
    [1]=>
    string(5) "Smith"
    [2]=>
    string(26) "example-free@wpdevelop.com"
  }
}

I have added this. +? \ ^ part to the regular expression, it matches the text between two ^ characters.

+1
source

All Articles