Untitled

mail@pastecode.io avatar
unknown
golang
a year ago
1.9 kB
4
Indexable
Never
```go
package main
import (    "fmt"
    "regexp"    "time"
)
type Flight struct {    AircraftType string
    Origin       string    Departure    time.Time
    Destination  string    Arrival      time.Time
}
func main() {    // Sample input data
    input := []string{        "Airbus-320 Tehran(Mon, Apr 7, 2025 7:36 PM) => Bandar-abbas",
        "Fokker-100 Tehran(Mon, Apr 7, 2025 7:37 PM) => Shiraz",        // Add more lines as needed
    }
    // Define the regular expression pattern    pattern := `^(\w+)-(\d+) (.+)\((.+)\) => (.+)`
    // Compile the regex pattern
    re := regexp.MustCompile(pattern)
    for _, line := range input {        // Find submatches in the line
        matches := re.FindStringSubmatch(line)
        if len(matches) != 6 {            fmt.Println("Invalid line:", line)
            continue        }
        // Extract data from submatches
        aircraftType := matches[1]        flightNumber := matches[2]
        origin := matches[3]        departureStr := matches[4]
        destination := matches[5]
        // Parse date and time strings into time.Time objects        departureTime, err := time.Parse("Mon, Jan 2, 2006 3:04 PM", departureStr)
        if err != nil {            fmt.Println("Error parsing departure time:", err)
            continue        }
        // Create Flight struct
        flight := Flight{            AircraftType: aircraftType + "-" + flightNumber,
            Origin:       origin,            Departure:    departureTime,
            Destination:  destination,            Arrival:      departureTime.Add(time.Hour), // Placeholder for arrival time
        }
        // Print the parsed data        fmt.Printf("Aircraft: %s, Origin: %s, Departure: %s, Destination: %s\n",
            flight.AircraftType, flight.Origin, flight.Departure, flight.Destination)    }
}
```