Splitting a string to extract numbersHi ,
I have a string that is character and I want to extract a 4 number section. The position of the numbers may change
An example of the string may be
value ="Hello 0123 example";
I have tried this but only works if the numbers are in the correct position.
value ="Hello 0123 example";
extract=substr(value,7, 4);
As the position of the number will change how can I extract this?
Advanced Statistical ModelingRe: Splitting a string to extract numbersok thank you @Aurora_TiffanyD and @jthi !
Re: Splitting a string to extract numbersThis is probably better because you don't have to grab the first match. Thanks!
Re: Splitting a string to extract numbersOne other option is to use Regex instead of Regex Match (there are also more options how this could be done, but I think Regex and Regex Match are the easiest ones)
Names Default To Here(1);
string = "Hello 0123 example";
four_num = Regex(string, "\d{4}"); //0123
See https://regex101.com/r/26hBdb/1 for how regex this works if you are not familiar with it.
Re: Splitting a string to extract numbersThis worked like a charm. Thank you so much!
Re: Splitting a string to extract numbersYou could try something like this:
Names Default To Here( 1 );
string = "Hello 0123 example";
matches = Regex Match(
string,
"[0-9]{4}",
);
// ASSUMES that you are interested only in the first 4-digit number, if there is more than one
extractedNumber = matches[1];
This returns "0123".
Please note that it still returns "0123" if your input string is "Hello 01234 example".