package main
import (
"fmt"
"regexp"
"sort"
"strconv"
)
/*
This program extracts all integer values from a mixed string
and sorts them using the language's built‑in sorting mechanism.
It demonstrates:
- clear separation of concerns using functions
- efficient number extraction using regular expressions
- dynamic storage using slices
- fast sorting with sort.Ints
*/
// ------------------------------------------------------------
// Extract all integer values from a mixed string.
// Uses a regular expression to find digit sequences.
// ------------------------------------------------------------
func extractNumbers(text string) []int {
// Find all sequences of digits in the string
re := regexp.MustCompile(`\d+`)
matches := re.FindAllString(text, -1)
// Convert each match to an integer
numbers := make([]int, 0, len(matches))
for _, m := range matches {
value, _ := strconv.Atoi(m)
numbers = append(numbers, value)
}
return numbers
}
// ------------------------------------------------------------
// Print all numbers in a space‑separated format.
// ------------------------------------------------------------
func printNumbers(numbers []int) {
for i, n := range numbers {
fmt.Print(n)
if i < len(numbers)-1 {
fmt.Print(" ")
}
}
fmt.Println()
}
// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
func main() {
text := "1000withz7 and3 or 99 give42"
// extract numbers
numbers := extractNumbers(text)
// sort numbers
sort.Ints(numbers)
// display result
fmt.Print("Sorted numbers: ")
printNumbers(numbers)
}
/*
run:
Sorted numbers: 3 7 42 99 1000
*/