How to remove all characters from a string, except the integers?
H. S. Teoh
hsteoh at quickfur.ath.cx
Fri Mar 4 14:40:58 UTC 2022
On Thu, Mar 03, 2022 at 06:36:35PM -0800, Ali Çehreli via Digitalmars-d-learn wrote:
> On 3/3/22 13:03, H. S. Teoh wrote:
>
> > string s = "blahblah123blehbleh456bluhbluh";
>
> > assert(result == 123456);
>
> I assumed it would generate separate integers 123 and 456. I started
> to implement a range with findSkip, findSplit, and friends but failed.
> :/
[...]
import std;
void main() {
string s = "blahblah123blehbleh456bluhbluh";
auto result = s.matchAll(regex(`\d+`))
.each!(m => writeln(m[0]));
}
Output:
123
456
Takes only 3 lines of code. ;-)
T
--
People demand freedom of speech to make up for the freedom of thought which they avoid. -- Soren Aabye Kierkegaard (1813-1855)
More information about the Digitalmars-d-learn
mailing list