Search code examples
regexstringformulalibreoffice-calc

Find position of first (from the right) numeric character in a text string


I have these strings:

fghs13412asdf
dfs234245gk

and want to return the position of the last numeric character, like so:

5
3

Perhaps there is something different in LibreOffice than Excel, where I'm seeing all the examples. Here's one that should be straightforward, and is returning an error.


Solution

  • Do you need the position of the first numeric character (as in the heading) or of the last one (as in the body of your question)?

    1. If it's the first one, a simple SEARCH() function using regular expressions should to the trick, e.g. =SEARCH("([:digit:])";A1).

    2. If it's the last one, counted from the start of the string, you can use a different regex (adapted from an answer in the OpenOffice forums by gerard24): =SEARCH("[0-9][^[0-9]]+$";A1).

    3. If you need the position of the last numeric character, counted from the end of the string, just subtract the value calculated in step 2 from the LEN() of the entire string: =LEN(A1)-(SEARCH("[0-9][^[0-9]]+$";A1)).

    You'll get a #VALUE! error if there's no numeric character, or if the last character of the input string is numeric. Note that whitespace in the string will be ignored:

    enter image description here