I have a list of string like this
$16,500,000(@$2,500)
$34,000(@$11.00)
$214,000(@$18.00)
$12,684,000(@$3,800)
How can I extract all symbols and the (@$xxxx) from these strings so that they can be like
16500000
34000
214000
12684000
\(.*?\)|\$|,
Try this.Replace by empty string.See demo.
https://regex101.com/r/vD5iH9/42
$re = "/\\(.*?\\)|\\$|,/m";
$str = "\$16,500,000(@\$2,500)\n\$34,000(@\$11.00)\n\$214,000(@\$18.00)\n\$12,684,000(@\$3,800)";
$subst = "";
$result = preg_replace($re, $subst, $str);
To remove the end (@$xxxx) characters, you could use the regex:
\(\@\$.+\)
and replace it with nothing:
preg_replace("/\(\@\$.+\)/g"), "", $myStringToReplaceWith)
Make sure to use the g (global) modifier so the regex doesn't stop after it finds the first match.
Here's a breakdown of that regex:
\( matches the ( character literally
\@ matches the @ character literally
\$ matches the $ character literally
.+ matches any character 1 or more times
\) matches the ) character literally
Here's a live example on regex101.com
In order to remove all of these characters:
$ , ( ) @ .
From a string, you could use the regex:
\$|\,|\(|\)|@|\.
Which will match all of the characters above.
The | character above is the regex or operator, effectively making it so
$ OR , OR ( OR ) OR @ OR . will be matched.
Next, you could replace it with nothing using preg_replace, and with the g (global) modifier, which makes it so the regex doesn't return on the first match:
preg_replace("/\$|\,|\(|\)|@|\./g"), "", $myStringToReplaceWith)
Here's a live example on regex101.com
So in the end, your code could look like this:
$str = preg_replace("/\(\@\$.+\)/g"), "", $str)
$str = preg_replace("/\$|\,|\(|\)|@|\./g"), "", $str)
Although it isn't in one regex, it does not use any look-ahead, or look-behind (both of which are not bad, by the way).
[^0-9]with nothing