Quantcast
Channel: Active questions tagged string-manipulation - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 189

Using NumberString, DigitCharacter, or a similar directive to find all string representations of integers in a string

$
0
0

I have a text file containing many, many lines of text like test in the following:

test = "word  123  456    7890.000      0.12000";

I would like to extract all of the "string representations of integers." However, I need to be clear about what I would like. In test above, I would like the output to be:

{"123", "456"}

since I am only interested in actual, isolated (delimited by spaces) string representations of integers. Yes, 7890 is an integer, but in test above, it is not isolated, so I do not want my function to return it (since 7890.000 is a decimal).

In the case of test, I could use this:

StringCases[test, Repeated[DigitCharacter, 3]][[1 ;; 2]]

which returns

{"123", "456"}

However, this is not general, because my string may contain more than two string representations of integers. So I would like my function to also take this input:

test = "word  123  456  123    7890.000      0.12000";

and return:

{"123", "456", "123"}

I have thought about using StringSplit followed by ToExpression and IntegerQ, but this seems like it would be very (unnecessarily?) complicated. Perhaps Mathematica has something better built in that I can use?

Do you have any advice? Thanks!


Viewing all articles
Browse latest Browse all 189

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>