Note that there are some explanatory texts on larger screens.

plurals
  1. PORegex end modifier not returning results in a Go program
    text
    copied!<p>I have a simple program in Go to aid in learning regular expressions. It runs in an infinite loop and has 2 channels, one which is used to provide input (input contains regex pattern and subject), and the second one, which provides the output.</p> <pre><code>usage: main.exe (cat)+ catcatdog </code></pre> <p>However there is propably something wrong in the code, as i can't seem to get any results with the <strong><code>$</code></strong> modifier.</p> <p>For example, i expect "cat" output from</p> <pre><code>main.exe cat$ cat\ndog </code></pre> <p>yet receive zero results.</p> <p>Code:</p> <pre><code>package main import ( "fmt" "regexp" "bufio" "os" "strings" ) type RegexRequest struct { regex string subject string } func main() { regexRequests := make(chan *RegexRequest) defer close(regexRequests) regexAnswers, err := createResolver(regexRequests) defer close(regexAnswers) if(err != nil) { // TODO: Panics when exited via ctrl+c panic(err) } interact(regexRequests, regexAnswers) } func interact(regexRequests chan *RegexRequest, regexAnswers chan []string) { for { fmt.Println("Enter regex and subject: ") reader := bufio.NewReader(os.Stdin) line, err := reader.ReadString('\n') if(err != nil) { panic(err) } regAndString := strings.SplitN(line, " ", 2); if len(regAndString) != 2 { fmt.Println("Invalid input, expected [regex][space][subject]") continue } regexRequests &lt;- &amp;RegexRequest{ regAndString[0], regAndString[1] } result := &lt;- regexAnswers var filteredResult []string for _, element := range result { if(element != "") { filteredResult = append(filteredResult, element) } else { filteredResult = append(filteredResult, "EMPTY"); } } fmt.Println(strings.Join(filteredResult, " ")) } } func createResolver(inputChan chan *RegexRequest)(outputChan chan []string, err error) { if(cap(inputChan) &gt; 0) { return nil, fmt.Errorf("Expected an unbuffered channel") } outputChan = make(chan []string) err = nil go func() { for { var regReq *RegexRequest= (&lt;- inputChan); var regex *regexp.Regexp = regexp.MustCompile(regReq.regex) outputChan &lt;- regex.FindAllString(regReq.subject, -1) } }() return } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload