import Foundation
let pattern = #"^\s*(\w*)=\s?\d"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
#!/bin/bash
flavor=${1:-dzen2}
function gmail() {
while true
do
inbox_count=$(wget -q -O- --auth-no-challenge --user=something@gmail.com --password=something 'https://mail.goog$
echo "Gg:$inbox_count"
sleep 60
done
}
function weather() {
while true
do
weather_stat=$($HOME/.config/i3/./weather.sh)
echo "F$weather_stat"
sleep 900
done
}
function time_date() {
while true
do
klocka=$(date +'%A den %d %B v%V %H:%M')
echo "S$klocka"
sleep 10
done < <(echo)
}
bspc control --put-status
xtitle -sf "T%s" > "$PANEL_FIFO" &
weather > "$PANEL_FIFO" &
gmail > "$PANEL_FIFO" &
time_date > "$PANEL_FIFO" &
case "$flavor" in
bar)
cat "$PANEL_FIFO" | .config/bspwm/panel_bar | bar
;;
dzen2)
. .config/bspwm/panel_colors
PANEL_HEIGHT=14
FONT_FAMILY='Dejavu Sans'
FONT_SIZE=9
cat "$PANEL_FIFO" | .config/bspwm/panel_dzen2 -f "$FONT_FAMILY" -s "$FONT_SIZE" | \
dzen2 -h $PANEL_HEIGHT -dock -ta l -title-name panel -fn "${FONT_FAMILY}:pixelsize=${FONT_SIZE}" \
-fg "$COLOR_FOREGROUND" -bg "$COLOR_BACKGROUND"
;;
esac
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression