regexp like preg_match

973 views
Skip to first unread message

GGGO

unread,
Nov 24, 2011, 6:50:54 PM11/24/11
to golang-nuts
I'm trying to understand package regexp, but I can't find a solution
to get data in a text like preg_match does in PHP.

preg_match('/^website: (.*) value="(.*)"/imU','website: sample.com
value="100"', $result)
$result[1] get the first (.*) => sample.com
$result[2] get the second (.*) => 100

Can I do the same thing with regexp ?

Thank you.

GGGO

unread,
Nov 24, 2011, 9:34:59 PM11/24/11
to golang-nuts
Ok I got it !
Hope it could help someone else.

r := regexp.MustCompile(`^website: (.*) value="(.*)"$`)
result := r.FindAllStringSubmatch(`website: www.sample.com
value="100"`,1)
fmt.Printf("Result1: %s\n",result[0][1]) // www.sample.com
fmt.Printf("Result2: %s\n",result[0][2]) // 100

FindAllStringSubmatch detects ( ) and return value of parenthesis

Comments are welcome.

Matthew R Chase

unread,
Jun 16, 2012, 10:16:16 AM6/16/12
to golan...@googlegroups.com
I've been having trouble with FindAllStringSubmatch.

Given a text string like:
_______________________________
word = some value
word = some other value
word = some|delimited|list
________________________________

I tried:
____________________________________________________
assigned := regexp.MustCompile(`^(\s*)(\w+?)(\s*)=(\s*)(.+?)$`)
matched := assigned.FindAllStringSubmatch(string(body), -1)
fmt.Println(string(body))
fmt.Println("Length of matched:", len(matched))
for i, v := range matched{
fmt.Println(i)
fmt.Println(v)
}
____________________________________________________
but would not find any matches.  if I changed my regex to `^(\s*)(\w+?)(\s*)=(\s*)(.+?)[\r\n]+`
it would find only one match.  I therefore do not understand the "All" part of "FindAllStringSubmatch"

I also don't understand why I had to use [\r\n]+ instead of $, but that's not as important.

thanks for your insight!

DisposaBoy

unread,
Jun 16, 2012, 10:43:31 AM6/16/12
to golan...@googlegroups.com
You're telling it to match at the start and end with ^ and $, if you want them to bound to the line then you need to tell it that e.g MustCompile(`(?m)^(\s*)... see http://code.google.com/p/re2/wiki/Syntax  

Matthew R Chase

unread,
Jun 16, 2012, 11:02:26 AM6/16/12
to golan...@googlegroups.com
I found it was problem with terminator.  and you're right -- multi-line mode was the answer.
Thanks!

Revised examples:
__________________________________________________________________
//Method 1: split for lines.
assigned := regexp.MustCompile(`^(?:\s*)(\w+?)(?:\s*)=(?:\s*)(.+?)$`)
lines := strings.Split(body, "\n")  //assumes no \r
for i,v := range lines{
fmt.Println("body[" + strconv.Itoa(i) + "] is:", v)
matched := assigned.FindStringSubmatch(v)
if(len(matched) == 3){
fmt.Println("  - " + matched[1] + ":" + matched[2])
}
}
//Method 2 (more better): multi-line Regex mode.
assigned = regexp.MustCompile(`(?m)^(?:\s*)(\w+?)(?:\s*)=(?:\s*)(.+?)$`)
matched  := assigned.FindAllStringSubmatch(body, -1)
fmt.Println("Length of matched:", len(matched))
for i, v := range matched{
fmt.Println("line[" + strconv.Itoa(i) + "] matched: ", v[1], ":", v[2])
}
__________________________________________________________________
Reply all
Reply to author
Forward
0 new messages