Lets assume I have a string like this
1234hello567u8 915 kl15
I want to match all the numbers up until the first space (So, 12345678)
I know I can use this: [^\s]* to match everything until the first space.. But how
How do i use [^\s]* to only match numbers?
In PHP you can use this:
$re = '/\h.*|\D+/';
$str = "1234hello567u8 915 kl15";
$result = preg_replace($re, '', $str);
//=> 12345678
RegEx Demo
Regex is about matching a pattern but it seems like you don't know exactly pattern of your text.
I suggest you like this
Replace all [a-z] to "", by using
regex: "s/[a-z]//g"
output: "12345678 915 15"
Capture text you want,
regex: "(^\d+)"
output: "12345678"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With