Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract part of string in Golang?

Tags:

go

I'm learning Golang so I can rewrite some of my shell scripts.

I have URL's that look like this:

https://example-1.example.com/a/c482dfad3573acff324c/list.txt?parm1=value,parm2=value,parm3=https://example.com/a?parm1=value,parm2=value

I want to extract the following part:

https://example-1.example.com/a/c482dfad3573acff324c/list.txt

In a shell script I would do something like this:

echo "$myString" | grep -o 'http://.*.txt'

What is the best way to do the same thing in Golang, only by using the standard library?

like image 333
John Smith Avatar asked Jul 27 '16 23:07

John Smith


People also ask

How do I substring string in Golang?

Range Based Slicing Another method to extract a substring in Go is to use range-based slicing. We can specify the starting and stopping index for the target index we wish to extract. Using range-based slicing is one of the most effective ways of generating a substring in go.

How do you remove the last character of a string in Golang?

The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.

How do you check if a string starts with a substring in Golang?

In Golang strings, you can check whether the string begins with the specified prefix or not with the help of HasPrefix() function. This function returns true if the given string starts with the specified prefix and return false if the given string does not start with the specified prefix.

What is rune type in Go?

rune in Go is a data type that stores codes that represent Unicode characters. Unicode is actually the collection of all possible characters present in the whole world. In Unicode, each of these characters is assigned a unique number called the Unicode code point. This code point is what we store in a rune data type.


1 Answers

There are a few options:

// match regexp as in question
pat := regexp.MustCompile(`https?://.*\.txt`)
s := pat.FindString(myString)

// everything before the query 
s := strings.Split(myString, "?")[0] string

// same as previous, but avoids []string allocation
s := myString
if i := strings.IndexByte(s, '?'); i >= 0 {
    s = s[:i]
}

// parse and clear query string
u, err := url.Parse(myString)
u.RawQuery = ""
s := u.String()

The last option is the best because it will handle all possible corner cases.

try it on the playground

like image 140
Bayta Darell Avatar answered Sep 22 '22 13:09

Bayta Darell