I have a string like
"'Joe'&@[Uk Customers.First Name](contact:16[[---]]first_name) +@[Uk Customers.Last Name](contact:16[[---]]last_name)"
My requirement is start finding the pattern
@[A.B](contact:**digit**[[---]]**field**)
There can be many pattern in single string.
and replace it with a new string (entire pattern should be replaced) with a dynamic text generated by digit and field value
For an example for above string there are two matches
1st match is : array( digit => 16, field =>first_name)
2nd match is : array( digit => 16, field =>last_name)
and somewhere I have few rules which are
if digit is 16 and field is first_name replace pattern with "John" if digit is 16 and field is last_name replace pattern with "Doe"
so the output string will be "'Joe'&John+Doe"
Thanks in advance.
The matching part is fairly straightforward. This will do the trick:
@\[[^.]+\.[^.]+\]\(contact:(\d+)\[\[---\]\]([^)]+)\)
In PHP (and other languages that support named capture groups), you can do this to get the array to contain keys "digit" and "field":
@\[[^.]+\.[^.]+\]\(contact:(?<digit>\d+)\[\[---\]\](?<field>[^)]+)\)
Example PHP code:
$regex = '/@\[[^.]+\.[^.]+\]\(contact:(?<digit>\d+)\[\[---\]\](?<field>[^)]+)\)/';
$text = '"\'Joe\'&@[Uk Customers.First Name](contact:16[[---]]first_name) +@[Uk Customers.Last Name](contact:16[[---]]last_name)"';
preg_match_all($regex, $text, $matches, PREG_SET_ORDER);
var_dump($matches);
Result:
array(2) {
[0]=>
array(5) {
[0]=>
string(55) "@[Uk Customers.First Name](contact:16[[---]]first_name)"
["digit"]=>
string(2) "16"
[1]=>
string(2) "16"
["field"]=>
string(10) "first_name"
[2]=>
string(10) "first_name"
}
[1]=>
array(5) {
[0]=>
string(53) "@[Uk Customers.Last Name](contact:16[[---]]last_name)"
["digit"]=>
string(2) "16"
[1]=>
string(2) "16"
["field"]=>
string(9) "last_name"
[2]=>
string(9) "last_name"
}
}
I'm not real clear on the logic you want to use for the replacement, so I'm afraid I can't help there without some clarification.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.