openlang

extract the numeric parts from a string containing letters and digits

I am a beginer with Tcl. My string is "entry_rows_chunk2022vol34i9mn.txt", and I only want its numbers printed as "2022 34 9" What is the way to do this?

Thanks beforehand

The conversation continues 2

Follow the reasoning
  1. Permalink

    Try something like this... The approach is to use regsub command to swap every nonnumeric character for a space:

    set inputLine "entry_rows_chunk2022vol34i9mn.txt"
    regsub -all -nocase {[._a-z]} $inputLine { } digitsText
    lassign $digitsText firstNum nextNum lastNum
    puts "result = $firstNum $nextNum $lastNum"
    # result = 2022 34 9

    Using regexp command would probably be simpler, but I haven't worked that out yet.

    References: regsub (https://www.tcl.tk/man/tcl/TclCmd/regsub.html), lassign (https://www.tcl.tk/man/tcl/TclCmd/lassign.html) and regexp (https://www.tcl.tk/man/tcl/TclCmd/regexp.html) (if you'd like to try)

  2. Permalink

    I don't have an older Tcl version for testing, but give this a try:

    set matches [regexp -all -inline {\d+} $entryText]
    puts [join $matches]

Other sides of the same question

  1. Escaping percent characters when calling String.Format?

    My strings.xml file holds a SQL query, and I'm trying to construct the finished string in code with String.Format. The SELECT…

    Replies 3Latest activity java
  2. Formatting strings with F#

    I'm building an F# library for the first time Using string.Format gives me a complaint saying that the function doesn't exist…

    Replies 2Latest activity fsharp
  3. Extracting one section of a tcl string

    With tcl I'm attempting to break apart Reports/Report#2.rpt and keep just Report#2.rpt ; I tried this approach, but it produc…

    Replies 2Latest activity tcl