I have a small PHP script that runs through my apache log - and i'm trying to convert this script to Go. However I'm having some difficulties finding a good equivalent to the PHP function preg_match
.
In my PHP script I run a preg_match
on each line in the log file like this:
preg_match('/([.0-9]+) .*?\[([0-9a-zA-Z:\/+ ]+)\].*?"[A-Z]+ \/([^\/ ]+)\/([a-zA-Z0-9\-.]+).*" ([0-9]{3}) .*"(.*?)"$/', $line, $matches)
Running this expression on this log:
100.100.100.100 - - [23/Feb/2015:03:03:56 +0100] "GET /folder/file.mp3 HTTP/1.1" 206 5637064 "-" "AppleCoreMedia/1.0.0.12B466 (iPhone; U; CPU OS 8_1_3 like Mac OS X; da_dk)"
Returns the following array(where I am only really interested in [1-6]:
Array
(
[0] => 100.100.100.100 - - [23/Feb/2015:03:03:56 +0100] "GET /folder/file.mp3 HTTP/1.1" 206 5637064 "-" "AppleCoreMedia/1.0.0.12B466 (iPhone; U; CPU OS 8_1_3 like Mac OS X; da_dk)"
[1] => 100.100.100.100
[2] => 23/Feb/2015:03:03:56 +0100
[3] => folder
[4] => file.mp3
[5] => 206
[6] => AppleCoreMedia/1.0.0.12B466 (iPhone; U; CPU OS 8_1_3 like Mac OS X; da_dk)
)
So my question is - is there a good equivalent to this in Go? I have tried some of the different regexp methods but can't seem to find one thats working for me.
Thanks
Definition and Usage The preg_match() function returns whether a match was found in a string.
preg_match stops looking after the first match. preg_match_all , on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.
Return Values ¶ preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .
PHP | preg_match() Function. This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.
First you need to know that you might need to modify the regex pattern itself, since go's regex engine does not behave exactly the same as PHP's regex engine. Both are using PCRE regexes where PHP implements more features than go. However, your pattern from the example should work in go without modifications.
Here comes an example program in go that works like PHP's preg_match()
:
package main
import "fmt"
import "regexp"
func main() {
str := `100.100.100.100 - - [23/Feb/2015:03:03:56 +0100] "GET /folder/file.mp3 HTTP/1.1" 206 5637064 "-" "AppleCoreMedia/1.0.0.12B466 (iPhone; U; CPU OS 8_1_3 like Mac OS X; da_dk)"`
r, _ := regexp.Compile(`([.0-9]+) .*?\[([0-9a-zA-Z:\/+ ]+)\].*?"[A-Z]+ \/([^\/ ]+)\/([a-zA-Z0-9\-.]+).*" ([0-9]{3}) .*"(.*?)"$`)
// Using FindStringSubmatch you are able to access the
// individual capturing groups
for index, match := range r.FindStringSubmatch(str) {
fmt.Printf("[%d] %s\n", index, match)
}
}
Output:
[0] 100.100.100.100 - - [23/Feb/2015:03:03:56 +0100] "GET /folder/file.mp3 HTTP/1.1" 206 5637064 "-" "AppleCoreMedia/1.0.0.12B466 (iPhone; U; CPU OS 8_1_3 like Mac OS X; da_dk)"
[1] 100.100.100.100
[2] 23/Feb/2015:03:03:56 +0100
[3] folder
[4] file.mp3
[5] 206
[6] AppleCoreMedia/1.0.0.12B466 (iPhone; U; CPU OS 8_1_3 like Mac OS X; da_dk)
Please check the manual about go regexes: http://golang.org/pkg/regexp/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With