Question: I have a text line with a fixed given count of numbers and strings like this:

```
set line " 0.123 -0.1 +0.31 +1.4 foo bar "

```

I tried to use regexp to pull out all the numbers and 'bar'. I don't need the string 'foo'.

This finds a single number:
 ([-+]?(\d+.\d\*))

To capture the set of numbers I could concatenate the pattern but that looks ugly. Is there a way to trigger multiple matches of the number pattern and then a few single matches of the string pattern?

Like this:

```
set line " 0.123 -0.1 +0.31 +1.4 foo bar "
regexp {magic_pattern} $line dummy n0 n1 n2 n3 s0 s1
puts $n0
  0.123
 puts $n1
  -0.1
 puts $n2
  0.31
 puts $n3
  1.4
 puts $s0
  foo
 puts $s1
  bar

```

Numbers will be all floats with zeroes leading decimal point when applicable and no exponential notation. All will be base 10, no hex or binary.

Cheers,
Gert

Answer: 
Well, there's no other way than concatenating the regexp...

```
set line " 0.123 -0.1 +0.31 +1.4 foo bar "
regexp {([-+]?\d+\.\d*) ([-+]?\d+\.\d*) ([-+]?\d+\.\d*) ([-+]?\d+\.\d*) (\S+) (\S+)} $line dummy n0 n1 n2 n3 s0 s1

```

`\S+` will match any non-space character. I have used it assuming that neither `foo` nor `bar` can contain spaces. A better pattern might be required otherwise.

---

Or you could construct it in a different way:

```
set num {([-+]?\d+\.\d*)}
set name {(\S+)}
regexp "$num $num $num $num $name $name" $line dummy n0 n1 n2 n3 s0 s1

```
`split` and `lassign` would be easier:

```
set line " 0.123 -0.1 +0.31 +1.4 foo bar "
set fields [split [string trim $line]]
if {[llength $fields] != 6} continue   ;# handle missing fields somehow
lassign $fields n0 n1 n2 n3 s0 s1
puts "$n0:$n1:$n2:$n3:$s0:$s1"

```

```
0.123:-0.1:+0.31:+1.4:foo:bar

```

Otherwise if you need a regex:

```
set fields [regexp -all -inline {[+-]?(?:\d+\.?\d*|\d*\.\d+)|\S+} $line]
if {[llength $fields] != 6} continue   ;# handle missing fields somehow
puts $fields    

```

```
0.123 -0.1 +0.31 +1.4 foo bar

```