在本章中,我将在第一部分中共享字符串操纵作弊表,以便在使用字符串时保持参考,在自动化日常工作时,您将做很多事情。然后,您会发现一些有用的项目,以帮助您在第二部分中的弦乐操作学习。
。注意:这是备忘单之后的GitHub for the Project Section
i。 String Manipulation cheatsheet
将其打印出来,将其胶带胶带在您的桌子上,或将其放入您最喜欢的三环活页夹中。它几乎包含每个字符串操纵,并以表达式为例。
字符串文字
ii。项目
从剪贴板上的正则电子邮件查找器
在此项目中,我们将使用Regex来确定是否复制到剪贴板的文本是否包含电子邮件。在一串文本中查找电子邮件的模式很方便,因为您可能会收到基于共同特征对组有用的数据。
如果您有兴趣根据电子邮件构建具有注册 /登录输入字段的Web应用程序,则这对于确定输入是否正确。< / p>很有用。< / p>
注意:这个项目中有第三方软件包依赖关系,但我相信您记得如何处理该要求>
package main
import (
"fmt"
"regexp"
"strings"
"github.com/atotto/clipboard"
)
func main() {
// create email regexp
regMail, _ := regexp.Compile(`[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}`)
// read os buffer
// find email regexp
text, _ := clipboard.ReadAll()
var mailAddr []string
// found e-mail
if regMail.MatchString(text) {
mailAddr = regMail.FindAllString(text, -1)
}
// Print found e-mails on the terminal
if len(mailAddr) > 0 {
clipboard.WriteAll(strings.Join(mailAddr, "\n"))
fmt.Println("Copied to clipboard:")
fmt.Println(strings.Join(mailAddr, "\n"))
} else {
fmt.Println("No email addresses found.")
}
}
正则密码复杂性检查器
密码复杂性是您要具有应用程序的附加功能。这是因为对于网络罪犯而言,简单的密码更容易获得/猜测。
因此,明智的做法是执行至少11个字符的密码复杂性,并插入一些大写字母和数字。因此,让我们构建一个小程序以检查给定密码是否通过复杂性要求。
package main
import (
"fmt"
"os"
"regexp"
"flag"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %s -h\n", os.Args[0])
} else {
pass := flag.String("p", "", "get password")
flag.Parse()
regStr, _ := regexp.Compile(`([0-9a-zA-Z]){11,}`)
if regStr.MatchString(*pass) {
fmt.Println("Password ok")
} else {
fmt.Println("Bad password")
}
os.Exit(0)
}
}
运行程序并传递可能的密码应导致程序中的密码或不良密码响应:
t@m1 regexppass % go run main.go --p=23498aosethuaosthAT
Pass ok
t@m1 regexppass % go run main.go --p=2Aoeue
Bad password
t@m1 regexppass % go run main.go --p=2AoeueEEEE
Bad password
测验建造者
现在,这个项目是我们队列中第一个自动执行平凡工作任务的项目 - 您的老师朋友会爱您!
在这个项目中,您将在美国及其首都围绕美国建造测验生成器。但是,通常您将具有一个可以更改以生成不同类型的测验的测验软件的格式。也许,您会更改它以制作祖国及其州的首都。
package main
import (
"math/rand"
"os"
"strconv"
"strings"
"time"
)
func main() {
capitals := map[string]string{
"Alabama": "Montgomery",
"Alaska": "Juneau",
"Arizona": "Phoenix",
"Arkansas": "Little Rock",
"California": "Sacramento",
"Colorado": "Denver",
"Connecticut": "Hartford",
"Delaware": "Dover",
"Florida": "Tallahassee",
"Georgia": "Atlanta",
"Hawaii": "Honolulu",
"Idaho": "Boise",
"Illinois": "Springfield",
"Indiana": "Indianapolis",
"Iowa": "Des Moines",
"Kansas": "Topeka",
"Kentucky": "Frankfort",
"Louisiana": "Baton Rouge",
"Maine": "Augusta",
"Maryland": "Annapolis",
"Massachusetts": "Boston",
"Michigan": "Lansing",
"Minnesota": "Saint Paul",
"Mississippi": "Jackson",
"Missouri": "Jefferson City",
"Montana": "Helena",
"Nebraska": "Lincoln",
"Nevada": "Carson City",
"New Hampshire": "Concord",
"New Jersey": "Trenton",
"New Mexico": "Santa Fe",
"New York": "Albany",
"North Carolina": "Raleigh",
"North Dakota": "Bismarck",
"Ohio": "Columbus",
"Oklahoma": "Oklahoma City",
"Oregon": "Salem",
"Pennsylvania": "Harrisburg",
"Rhode Island": "Providence",
"South Carolina": "Columbia",
"South Dakota": "Pierre",
"Tennessee": "Nashville",
"Texas": "Austin",
"Utah": "Salt Lake City",
"Vermont": "Montpelier",
"Virginia": "Richmond",
"Washington": "Olympia",
"West Virginia": "Charleston",
"Wisconsin": "Madison",
"Wyoming": "Cheyenne",
}
var states, capitalsItems []string
for y, x := range capitals {
capitalsItems = append(capitalsItems, x)
states = append(states, y)
}
for i := 0; i < 35; i++ {
// сreate the quiz text file
str1 := "quiz_" + strconv.Itoa(i+1) + ".txt"
quizFile, err := os.Create(str1)
check(err)
defer quizFile.Close()
// create the answer key to the quiz
str2 := "answer_key_" + strconv.Itoa(i+1) + ".txt"
answerKeyFile, err := os.Create(str2)
check(err)
defer answerKeyFile.Close()
// Create portion for students to fill out
quizFile.WriteString("Student Number:\n\nName:\n\nDate:\n\n")
str3 := "Quiz " + strconv.Itoa(i+1)
quizFile.WriteString(strings.Repeat(" ", 20) + str3)
quizFile.WriteString("\n\n")
rand.Seed(time.Now().UnixNano())
// mix of the States
shuffle(states)
// Iterate through and build the question out
for j := 0; j < 50; j++ {
correctAnswer := capitals[states[j]]
wrongAnswers := make([]string, len(capitalsItems))
copy(wrongAnswers, capitalsItems)
// shuffle wrong answers
answNoCorrect := make([]string, len(wrongAnswers)-1)
for l := 0; l < len(wrongAnswers); l++ {
if wrongAnswers[l] == correctAnswer {
copy(answNoCorrect, removeAtIndex(wrongAnswers, l))
}
}
// create answer options A-D
var answerOptions []string
for l := 0; l < 3; l++ {
answerOptions = append(answerOptions, answNoCorrect[l])
}
answerOptions = append(answerOptions, correctAnswer)
shuffle(answerOptions)
// create question
str3 := strconv.Itoa(j+1) + " What is the Capital of " + states[j] + "?" + "\n"
quizFile.WriteString(str3)
strAbcd := "ABCD"
for l := 0; l < 4; l++ {
strAnsw := string(strAbcd[l]) + ". " + answerOptions[l] + "\n"
quizFile.WriteString(strAnsw)
}
// make quiz and save it
quizFile.WriteString("\n")
// make answer key and save it
strAnswerOk := ""
for l := 0; l < len(answerOptions); l++ {
if answerOptions[l] == correctAnswer {
strAnswerOk += string(strAbcd[l])
}
}
strCorAnsw := strconv.Itoa(j+1) + ". " + strAnswerOk + "\n"
answerKeyFile.WriteString(strCorAnsw)
}
}
}
// helper functions for making quiz building easier
func check(e error) {
if e != nil {
panic(e)
}
}
func shuffle(a []string) {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
}
func removeAtIndex(source []string, index int) []string {
lastIndex := len(source) - 1
source[index], source[lastIndex] = source[lastIndex], source[index]
return source[:lastIndex]
}
您拥有它,您将使用的所有字符串知识的基础与文件,剪贴板或其他任何地方的字符串数据一起工作。