I want to replace any non-alphanumeric character sequences with a dash. A snippet of what I wrote is below. However it does not work and I'm completely clueless why. Could anyone explain me why the snippet behaves not like I expect it to and what would be the correct way to accomplish this?
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
reg, _ := regexp.Compile("/[^A-Za-z0-9]+/")
safe := reg.ReplaceAllString("a*-+fe5v9034,j*.AE6", "-")
safe = strings.ToLower(strings.Trim(safe, "-"))
fmt.Println(safe) // Output: a*-+fe5v9034,j*.ae6
}
The forward slashes are not matched by your string.
package main
import (
"fmt"
"log"
"regexp"
"strings"
)
func main() {
reg, err := regexp.Compile("[^A-Za-z0-9]+")
if err != nil {
log.Fatal(err)
}
safe := reg.ReplaceAllString("a*-+fe5v9034,j*.AE6", "-")
safe = strings.ToLower(strings.Trim(safe, "-"))
fmt.Println(safe) // Output: a*-+fe5v9034,j*.ae6
}
(Also here)
Output
a-fe5v9034-j-ae6
Related
This question already has answers here:
How to extract a floating number from a string [duplicate]
(7 answers)
Closed 2 years ago.
I wrote in Go the following code to extract two values inside the string.
I used two regexp to seek the numbers (float64).
The first result is the correct, only de number. But the second is wrong.
This is the code:
package main
import (
"fmt"
"regexp"
)
func main() {
// RegExp utiliza la sintaxis RE2
pat1 := regexp.MustCompile(`[^m2!3d][\d\.-]+`)
s1 := pat1.FindString(`Torre+Eiffel!8m2!3d-48.8583701!4d-2.2944813!3m4!1s0x47e66e2964e34e2d:0x8ddca9ee380ef7e0!8m2!3d-48.8583701!4d-2.2944813`)
pat2 := regexp.MustCompile(`[^!4d][\d\.-]+`)
s2 := pat2.FindString(`Torre+Eiffel!8m2!3d-48.8583701!4d-2.2944813!3m4!1s0x47e66e2964e34e2d:0x8ddca9ee380ef7e0!8m2!3d-48.8583701!4d-2.2944813`)
fmt.Println(s1) // Print -> -48.8583701
fmt.Println(s2) // Print -> m2 (The correct answer is "-2.2944813")
}
Here I modify the syntax
pat2 := regexp.MustCompile(!4d[\d\.-]+)
and I get the following answer:
!4d-2.2944813
but it's not what I'm expecting.
It seems like you are only interessed in the latitude and longitute of an attraction and not really in the regex.
Maybe you just use something like this:
package main
import (
"fmt"
"strconv"
"strings"
)
var replacer = strings.NewReplacer("3d-", "", "4d-", "")
func main() {
var str = `Torre+Eiffel!8m2!3d-48.8583701!4d-2.2944813!3m4!1s0x47e66e2964e34e2d:0x8ddca9ee380ef7e0!8m2!3d-48.8583701!4d-2.2944813`
fmt.Println(getLatLong(str))
}
func getLatLong(str string) (float64, float64, error) {
parts := strings.Split(str, "!")
if latFloat, err := strconv.ParseFloat(replacer.Replace(parts[2]), 64); err != nil {
return 0, 0, err
} else if lngFloat, err := strconv.ParseFloat(replacer.Replace(parts[3]), 64); err != nil {
return 0, 0, err
} else {
return latFloat, lngFloat, nil
}
}
https://play.golang.org/p/UOIwGbl6nrb
You where almost there. Try (?m)(?:3d|4d)-([\d\.-]+)(?:!|$)
https://regex101.com/r/8KgirB/1
All you need is a matching group around the [\d\.-]+ part. With this group you are able to access it directly
package main
import (
"fmt"
"regexp"
)
func main() {
var re = regexp.MustCompile(`(?m)(?:3d|4d)-([\d\.-]+)!`)
var str = `Torre+Eiffel!8m2!3d-48.8583701!4d-2.2944813!3m4!1s0x47e66e2964e34e2d:0x8ddca9ee380ef7e0!8m2!3d-48.8583701!4d-2.2944813`
for _, match := range re.FindAllStringSubmatch(str, -1) {
fmt.Println(match[1])
}
}
This question already has answers here:
Regexp won't match
(2 answers)
Closed 4 years ago.
I am receiving this string "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80J\x13\x80SQ\x80L\xe0\x80#\x92\x80L?\x80H\xe0" from a function (which runs a GET command on a redis bitmap and gives me the serialized string)
But due to escape sequences I am having trouble matching this kind of pattern. Can some please tell me the regex sequence that will match this kind of string?
First, I'd try to find out how to get that same data from Redis in its direct, binary, form (as []byte); the rest would be way more simple then.
But to deal with this stuff in its present form,
I would first normalize the input string—replacing all those
backslash-escaped hex-encoded characters with their equivalent bytes.
This would allow easily searchig for the exact values of these
bytes—possibly using backslash-escaped hex-encoded characters
in the patterns:
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
s := "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80J\x13\x80SQ\x80L\xe0\x80#\x92\x80L?\x80H\xe0"
s, err := strconv.Unquote(`"` + s + `"`)
if err != nil {
panic(err)
}
fmt.Println(strings.Index(s, "\x80SQ\x80L"))
}
Playground link.
An introduction to Redis data types and abstractions
Bitmaps
Bitmaps are not an actual data type, but a set of bit-oriented
operations defined on the String type.
Regular expressions are not the best solution. Write a simple Go function to do the conversion. For example,
package main
import (
"fmt"
"strconv"
)
func redisBits(s string) (string, error) {
s, err := strconv.Unquote(`"` + s + `"`)
if err != nil {
return "", err
}
return s, nil
}
func main() {
s := "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80J\x13\x80SQ\x80L\xe0\x80#\x92\x80L?\x80H\xe0"
fmt.Printf("%x\n", s)
b, err := redisBits(s)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%x\n", b)
}
Playground: https://play.golang.org/p/rbE9iG3tOTx
Output:
0100000000000000000000804a13805351804ce0804092804c3f8048e0
0100000000000000000000804a13805351804ce0804092804c3f8048e0
I tried something I did in Javascript.
But it says
http://play.golang.org/p/qlWLI03Dnl
package main
import "fmt"
import "regexp"
import "strings"
func swapit(str string) string {
var validID = regexp.MustCompile(`[a-z]|[A-Z]`)
return validID.ReplaceAllString(str, func(${0}, ${1}, ${2}) string {
return (${1}) ? strings.ToUpper(${0}) : strings.ToLower(${0})
})
}
func main() {
fmt.Println(swapit("hello wOrld."))
// HELLO WoRLD.
}
I also tried this removing ? : syntax but still does not work.
http://play.golang.org/p/mD6_78zzo1
Does really go not support this? Do I just give up and just bruteforce each character to change cases?
Thanks a lot
As #James Henstridge already pointed out, there are multiple problems with your code. This answer will not focus on the errors, but rather a different way of solving the problem.
If your aim is to learn about using regexp in Go, this answer of mine is useless.
If your aim is to get learn how to make a function that swaps cases, then I suggest a solution without regexp, utilizing the unicode package instead:
package main
import (
"bytes"
"fmt"
"unicode"
)
func SwapCase(str string) string {
b := new(bytes.Buffer)
for _, r := range str {
if unicode.IsUpper(r) {
b.WriteRune(unicode.ToLower(r))
} else {
b.WriteRune(unicode.ToUpper(r))
}
}
return b.String()
}
func main() {
fmt.Println(SwapCase("Hej värLDen."))
}
Output:
hEJ VÄRldEN.
Playground
This solution will handle all non A-Z characters as well, such as ö-Ö and å-Å.
I want to replace regex-matched strings with new string but still keeping part of the original text.
I want to get
I own_VERB it and also have_VERB it
from
I own it and also have it
how do I do this with one line of code? I tried but can't get further than this. Thanks,
http://play.golang.org/p/SruLyf3VK_
package main
import "fmt"
import "regexp"
func getverb(str string) string {
var validID = regexp.MustCompile(`(own)|(have)`)
return validID. ReplaceAllString(str, "_VERB")
}
func main() {
fmt.Println(getverb("I own it and also have it"))
// how do I keep the original text like
// I own_VERB it and also have_VERB it
}
You don't even need a capture group for this.
package main
import "fmt"
import "regexp"
func getverb(str string) string {
var validID = regexp.MustCompile(`own|have`)
return validID. ReplaceAllString(str, "${0}_VERB")
}
func main() {
fmt.Println(getverb("I own it and also have it"))
// how do I keep the original text like
// I own_VERB it and also have_VERB it
}
${0} contains the string which matched the whole pattern; ${1} will contain the string which matched the first sub-pattern (or capture group) if there is any, and which you can see in Darka's answer.
Seems a bit googling helps:
var validID = regexp.MustCompile(`(own|have)`)
return validID. ReplaceAllString(str, "${1}_VERB")
Inside replacement, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile("(own|have)")
fmt.Println(re.ReplaceAllString("I own it and also have it", "${1}_VERB"))
}
Output
I own_VERB it and also have_VERB it
As a personal project, trying to learn Go(lang) by applying it to something, I am writing an EMCAScript/JavaScript "compiler"; all it will (initially) do is allow you to include other .js files.
Functionality aside, I am pulling my hair out trying to figure out the regexp package. Here is the snippet that does not seem to be doing what I want it to:
// Note: "lines" is an array of strings.
var includeRegex, _ = regexp.Compile("^[ \t]*include[(]{1}\"([^\"]+)\"[)]{1};")
for _, line := range lines {
var isInclude = includeRegex.Match([]byte(line))
if isInclude {
var includeFile = includeRegex.FindString(line)
fmt.Println("INCLUDE", includeFile)
} else {
// ...
}
I have already stumbled across Go's subset of regular expressions, hence why the regex does not read as ^\s*include\("([^"]+)"\);. I have already tested both the preferred, and the Go-style regex, in RegexPal, and both definitely work. The match just never seems to occurr; what am I doing wrong?
For what it's worth, the include() statement I am trying to parse looks like so:
include("somefile.js");
EDIT: For what it's worth, I am keeping the code here.
This seems to work with the latest weekly
package main
import (
"fmt"
"log"
"regexp"
"strings"
)
func main() {
includeRegex, err := regexp.Compile(`^\s*include\("(\\\"|[^"])+"\);`)
if err != nil {
log.Fatal(err)
}
for _, line := range strings.Split(`
foo
include "abc.def"
include("file.js");
include "me\"to\""
include("please\"!\"");
nothing here
`, "\n") {
if includeRegex.Match([]byte(line)) {
includeFile := includeRegex.FindString(line)
fmt.Println("INCLUDE", includeFile)
} else {
fmt.Printf("no match for \"%s\"\n", line)
}
}
}
Output:
$ go build && ./tmp
no match for ""
no match for "foo"
no match for "include "abc.def""
INCLUDE include("file.js");
no match for " include "me\"to\"""
INCLUDE include("please\"!\"");
no match for " nothing here "
no match for ""
$
Try putting the following line at the start of your program:
println(runtime.Version())
It should print weekly.2012-03-13 or something close to that date.