(s string)
| 252 | } |
| 253 | |
| 254 | func SplitButRespectQuotes(s string) []string { |
| 255 | |
| 256 | // This regex matches either a quoted string (with either single or double quotes) or a non-space sequence. |
| 257 | // For example, for the input: `hello "my name" is 'Sammy'` |
| 258 | // It matches: [`hello", ""my name"", "is", "'Sammy'`] |
| 259 | re := regexp.MustCompile(`("[^"]*")|('[^']*')|\S+`) |
| 260 | matches := re.FindAllString(s, -1) |
| 261 | finalMatches := make([]string, 0, 1) |
| 262 | |
| 263 | // Remove quotes around the matches, if they exist |
| 264 | for _, match := range matches { |
| 265 | |
| 266 | match = strings.TrimSpace(match) |
| 267 | |
| 268 | if strings.HasPrefix(match, `"`) && strings.HasSuffix(match, `"`) || |
| 269 | strings.HasPrefix(match, `'`) && strings.HasSuffix(match, `'`) { |
| 270 | str := strings.TrimSpace(match[1 : len(match)-1]) |
| 271 | finalMatches = append(finalMatches, str) |
| 272 | } else { |
| 273 | finalMatches = append(finalMatches, match) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return finalMatches |
| 278 | } |
| 279 | |
| 280 | // accepts an input and splits it along a # if any. |
| 281 | // By default returns the full string and 1 as the number. |
no outgoing calls