...or if you copy the strings.Contains() comparison to the test, it echoes
the program with six and each of the patterns removed.

On Sun, Apr 23, 2017 at 7:57 AM, Michael Jones <michael.jo...@gmail.com>
wrote:

> Another example:
> https://play.golang.org/p/hDKQtwHo6M
>
> In this case, just copy the program to your machine and build it.
> i called it lines.go
>
> when i run it as:
> lines < lines.go
>
> it echos the program with the line "six" removed
>
> On Sun, Apr 23, 2017 at 12:47 AM, Ishan Jain <ishanjai...@gmail.com>
> wrote:
>
>> You can do it like this. I wrote the code to filter out lines from an
>> array that does not contain words specified in array.
>>
>> package main
>>>
>>> import (
>>>    "os"
>>>    "log"
>>>    "bufio"
>>>    "io"
>>>    "regexp"
>>> )
>>>
>>> var abcd = [5]string{
>>>    "one two",
>>>    "three four fix",
>>>    "six",
>>>    "seven eight nine ten",
>>>    "eleven twelve thirteen fourteen fifteen",
>>> }
>>>
>>> func main() {
>>>
>>>    inputFile, err := os.Open("file.txt")
>>>    if err != nil {
>>>       log.Fatalln(err)
>>>    }
>>>
>>>    reader := bufio.NewReader(inputFile)
>>>
>>>    for {
>>>       filedata, _, err := reader.ReadLine()
>>>       if err != nil {
>>>          if err != io.EOF {
>>>             log.Fatal(err)
>>>          }
>>>          return
>>>       }
>>>
>>>       if stringsInSlice(string(filedata)) {
>>>
>>>          // Write This Line to a file.
>>>
>>>       }
>>>    }
>>> }
>>>
>>> func stringsInSlice(str string) bool {
>>>    for _, v := range abcd {
>>> // \b is used to create boundary, So, It won't match a word in a bigger 
>>> word.
>>>       regex, err := regexp.Compile(`\b` + v + `\b`)
>>>       if err != nil {
>>>          log.Panic("Error In Compiling Regex")
>>>       }
>>>       if len(regex.FindAllString(str, 1)) != 0 {
>>>          return true
>>>       }
>>>    }
>>>    return false
>>> }
>>>
>>>
>> See this code in Action.
>>
>> https://play.golang.org/p/v-rzh-Wpui
>>
>> If there is something in my code that you did not understood, please let
>> me know.
>>
>> Regards
>>
>> Ishan Jain
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Michael T. Jones
> michael.jo...@gmail.com
>



-- 
Michael T. Jones
michael.jo...@gmail.com

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to