How to extract positive, negative and fraction numbers from string?
Show older comments
How do I extract numbers from a string where the number can be positive, negative or even fractions?
I tried the following but it is not getting the fractions. Thanks.
B = str2double(regexp(str,'[+-]?\d+','match'));
4 Comments
Walter Roberson
on 9 Mar 2022
When there is a fraction, is the input permitted to omit leading 0? 0.5 vs .5 ? The expression is easier if the 0 would be required.
Is it necessary to support scientific notation?
Image Analyst
on 9 Mar 2022
Give some examples of what the strings might be.
Stephen23
on 9 Mar 2022
See also comment from @Nadatimuj here: https://www.mathworks.com/matlabcentral/answers/406543-how-to-extract-only-floating-numbers-from-a-string#comment_2028794
The answer there also answers this question.
Nadatimuj
on 9 Mar 2022
Accepted Answer
More Answers (1)
Since it was never clarified, I'm going to ignore sci/eng notation:
str = '+100 -100 100 100.1 100.001 1.001 .001 100.';
B = str2double(regexp(str,'[+-]?(\d*\.)?\d+','match'))
Note how this will behave on things which are not decimal expressions:
str = '11.22.33.44.55';
B = str2double(regexp(str,'[+-]?(\d*\.)?\d+','match'))
I'm sure it can be more robust, but it's a start.
12 Comments
Walter Roberson
on 9 Mar 2022
The match on 100. is accidental, ending the match before the period.
Walter Roberson
on 9 Mar 2022
It mostly matters if the person need to match something immediately afterdwards.
str = '+100 -100 100 100.1 100.001 1.001 .001 100. -.5';
regexp(str, '([-+]?\d+(\.\d*)?)|([-+]?\.\d+)', 'match')
str = '+100 -100 100 100.1 100.001 1.001 .001 100. -.5';
regexp(str, '[-+]?(\d+\.?\d*|\.\d+)', 'match')
That does look more compact.
str = '+100 -100 100 100.1 100.001 1.001 .001 100. -.5';
regexp(str, '[-+]?(\d+(\.\d*)?|\.\d+)', 'match')
Not much difference, but as a matter of style if the . is not present, I prefer not to make it work with the \d* after that.
S = '{(0, 0): -8, (0, 1): 2, (0, 2): 2, (0, 3): 2, (0, 4): 2, (0, 5): 2, (0, 6): 2, (0, 7): 2, (0, 8): 4, (0, 9): 8, (1, 1): -12, (1, 2): 2, (1, 3): 2, (1, 4): 2, (1, 5): 2, (1, 6): 2, (1, 7): 2, (1, 8): 4, (1, 9): 8, (2, 2): -10, (2, 3): 2, (2, 4): 2, (2, 5): 2, (2, 6): 2, (2, 7): 2, (2, 8): 4, (2, 9): 8, (3, 3): -8, (3, 4): 2, (3, 5): 2, (3, 6): 2, (3, 7): 2, (3, 8): 4, (3, 9): 8, (4, 4): -8, (4, 5): 2, (4, 6): 2, (4, 7): 2, (4, 8): 4, (4, 9): 8, (5, 5): -10, (5, 6): 2, (5, 7): 2, (5, 8): 4, (5, 9): 8, (6, 6): -8, (6, 7): 2, (6, 8): 4, (6, 9): 8, (7, 7): -9, (8, 8): -16, (9, 9): -24, (7, 8): 4, (7, 9): 8, (8, 9): 16, (10, 10): 58.0, (0, 10): -2, (1, 11): 2, (11, 11): 56.0, (2, 12): 2, (12, 12): 56.0, (13, 13): 58.0, (3, 13): -2, (14, 14): 58.0, (4, 14): -2, (15, 15): 58.0, (5, 15): -2, (16, 16): 58.0, (6, 16): -2, (17, 17): -55.0, (17, 18): 4, (17, 19): 8, (17, 20): 16, (17, 21): 32, (17, 22): 64, (10, 17): -2, (11, 17): -2, (12, 17): -2, (13, 17): -2, (14, 17): -2, (15, 17): -2, (16, 17): -2, (18, 18): -108.0, (18, 19): 16, (18, 20): 32, (18, 21): 64, (18, 22): 128, (10, 18): -4, (11, 18): -4, (12, 18): -4, (13, 18): -4, (14, 18): -4, (15, 18): -4, (16, 18): -4, (19, 19): -208.0, (19, 20): 64, (19, 21): 128, (19, 22): 256, (10, 19): -8, (11, 19): -8, (12, 19): -8, (13, 19): -8, (14, 19): -8, (15, 19): -8, (16, 19): -8, (20, 20): -384.0, (20, 21): 256, (20, 22): 512, (10, 20): -16, (11, 20): -16, (12, 20): -16, (13, 20): -16, (14, 20): -16, (15, 20): -16, (16, 20): -16, (21, 21): -640.0, (21, 22): 1024, (10, 21): -32, (11, 21): -32, (12, 21): -32, (13, 21): -32, (14, 21): -32, (15, 21): -32, (16, 21): -32, (22, 22): -771.0, (10, 22): -64, (11, 22): -64, (12, 22): -64, (13, 22): -64, (14, 22): -64, (15, 22): -64, (16, 22): -64, (10, 11): 2, (10, 12): 2, (10, 13): 2, (10, 14): 2, (10, 15): 2, (10, 16): 2, (11, 12): 2, (11, 13): 2, (11, 14): 2, (11, 15): 2, (11, 16): 2, (12, 13): 2, (12, 14): 2, (12, 15): 2, (12, 16): 2, (13, 14): 2, (13, 15): 2, (13, 16): 2, (14, 15): 2, (14, 16): 2, (15, 16): 2, (23, 23): 60.0, (0, 23): -2, (1, 24): 2, (24, 24): 58.0, (2, 25): 2, (25, 25): 58.0, (26, 26): 60.0, (3, 26): -2, (4, 27): 2, (27, 27): 58.0, (5, 28): 2, (28, 28): 58.0, (29, 29): 60.0, (6, 29): -2, (30, 30): -57.0, (30, 31): 4, (30, 32): 8, (30, 33): 16, (30, 34): 32, (30, 35): 64, (23, 30): -2, (24, 30): -2, (25, 30): -2, (26, 30): -2, (27, 30): -2, (28, 30): -2, (29, 30): -2, (31, 31): -112.0, (31, 32): 16, (31, 33): 32, (31, 34): 64, (31, 35): 128, (23, 31): -4, (24, 31): -4, (25, 31): -4, (26, 31): -4, (27, 31): -4, (28, 31): -4, (29, 31): -4, (32, 32): -216.0, (32, 33): 64, (32, 34): 128, (32, 35): 256, (23, 32): -8, (24, 32): -8, (25, 32): -8, (26, 32): -8, (27, 32): -8, (28, 32): -8, (29, 32): -8, (33, 33): -400.0, (33, 34): 256, (33, 35): 512, (23, 33): -16, (24, 33): -16, (25, 33): -16, (26, 33): -16, (27, 33): -16, (28, 33): -16, (29, 33): -16, (34, 34): -672.0, (34, 35): 1024, (23, 34): -32, (24, 34): -32, (25, 34): -32, (26, 34): -32, (27, 34): -32, (28, 34): -32, (29, 34): -32, (35, 35): -829.0, (23, 35): -64, (24, 35): -64, (25, 35): -64, (26, 35): -64, (27, 35): -64, (28, 35): -64, (29, 35): -64, (23, 24): 2, (23, 25): 2, (23, 26): 2, (23, 27): 2, (23, 28): 2, (23, 29): 2, (24, 25): 2, (24, 26): 2, (24, 27): 2, (24, 28): 2, (24, 29): 2, (25, 26): 2, (25, 27): 2, (25, 28): 2, (25, 29): 2, (26, 27): 2, (26, 28): 2, (26, 29): 2, (27, 28): 2, (27, 29): 2, (28, 29): 2, (0, 36): 2, (36, 36): 54.0, (1, 37): 2, (37, 37): 54.0, (38, 38): 56.0, (2, 38): -2, (3, 39): 2, (39, 39): 54.0, (40, 40): 56.0, (4, 40): -2, (5, 41): 2, (41, 41): 54.0, (6, 42): 2, (42, 42): 54.0, (43, 43): -53.0, (43, 44): 4, (43, 45): 8, (43, 46): 16, (43, 47): 32, (43, 48): 64, (36, 43): -2, (37, 43): -2, (38, 43): -2, (39, 43): -2, (40, 43): -2, (41, 43): -2, (42, 43): -2, (44, 44): -104.0, (44, 45): 16, (44, 46): 32, (44, 47): 64, (44, 48): 128, (36, 44): -4, (37, 44): -4, (38, 44): -4, (39, 44): -4, (40, 44): -4, (41, 44): -4, (42, 44): -4, (45, 45): -200.0, (45, 46): 64, (45, 47): 128, (45, 48): 256, (36, 45): -8, (37, 45): -8, (38, 45): -8, (39, 45): -8, (40, 45): -8, (41, 45): -8, (42, 45): -8, (46, 46): -368.0, (46, 47): 256, (46, 48): 512, (36, 46): -16, (37, 46): -16, (38, 46): -16, (39, 46): -16, (40, 46): -16, (41, 46): -16, (42, 46): -16, (47, 47): -608.0, (47, 48): 1024, (36, 47): -32, (37, 47): -32, (38, 47): -32, (39, 47): -32, (40, 47): -32, (41, 47): -32, (42, 47): -32, (48, 48): -703.0, (36, 48): -64, (37, 48): -64, (38, 48): -64, (39, 48): -64, (40, 48): -64, (41, 48): -64, (42, 48): -64, (36, 37): 2, (36, 38): 2, (36, 39): 2, (36, 40): 2, (36, 41): 2, (36, 42): 2, (37, 38): 2, (37, 39): 2, (37, 40): 2, (37, 41): 2, (37, 42): 2, (38, 39): 2, (38, 40): 2, (38, 41): 2, (38, 42): 2, (39, 40): 2, (39, 41): 2, (39, 42): 2, (40, 41): 2, (40, 42): 2, (41, 42): 2, (22, 49): 2, (50, 50): 2, (35, 50): -2, (48, 51): 2, (52, 52): 1, (52, 53): 4, (49, 52): -2, (50, 52): -2, (51, 52): -2, (53, 53): 5, (49, 53): -4, (50, 53): -4, (51, 53): -4, (49, 50): 2, (49, 51): 2, (50, 51): 2, (22, 54): 2, (55, 55): 2, (35, 55): -2, (56, 56): 2, (48, 56): -2, (57, 57): 1, (57, 58): 4, (54, 57): -2, (55, 57): -2, (56, 57): -2, (58, 58): 3, (54, 58): -4, (55, 58): -4, (56, 58): -4, (54, 55): 2, (54, 56): 2, (55, 56): 2, (22, 59): 2, (60, 60): 2, (35, 60): -2, (61, 61): 2, (48, 61): -2, (62, 62): 1, (62, 63): 4, (59, 62): -2, (60, 62): -2, (61, 62): -2, (63, 63): 5, (59, 63): -4, (60, 63): -4, (61, 63): -4, (59, 60): 2, (59, 61): 2, (60, 61): 2, (64, 64): 2, (53, 64): -2, (58, 65): 2, (66, 66): 2, (63, 66): -2, (67, 67): 1, (64, 67): -2, (65, 67): -2, (66, 67): -2, (64, 65): 2, (64, 66): 2, (65, 66): 2}'
parts = regexp(S, '(?<c1>\d+),\s+(?<c2>\d+)\):\s+(?<val>[^,\}]+)', 'names')
all_c1 = str2double({parts.c1})
all_c2 = str2double({parts.c2})
all_val = str2double({parts.val})
Nadatimuj
on 9 Mar 2022
Walter Roberson
on 9 Mar 2022
I copied into an Answer.
DGM
on 9 Mar 2022
I didn't even think of using an OR.
From a beginner's perspective, regex feels like a swiss army knife with so many blades that you end up just using the knife blade as a screwdriver because you can't remember which one was the screwdriver blade.
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!