- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Splitting a string to extract numbers
Hi ,
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?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Splitting a string to extract numbers
You 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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Splitting a string to extract numbers
One 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Splitting a string to extract numbers
You 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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Splitting a string to extract numbers
This worked like a charm. Thank you so much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Splitting a string to extract numbers
One 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Splitting a string to extract numbers
This is probably better because you don't have to grab the first match. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Splitting a string to extract numbers
ok thank you @Aurora_TiffanyD and @jthi !