3

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
1
  • hint: replace anything that isn't a number [^0-9] with nothing Commented Feb 3, 2015 at 4:22

2 Answers 2

1
\(.*?\)|\$|,

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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).

2 Comments

thank you for your answer, but i don't need anything inside the bracket
Looks like I misread the question. I just updated the answer to make it more on-topic (deletion would work, but not deleting it could help others that come to look at this question). @vks has the better answer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.