本文整理汇总了Golang中regexp.Compile函数的典型用法代码示例。如果您正苦于以下问题:Golang Compile函数的具体用法?Golang Compile怎么用?Golang Compile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Compile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
directory := flag.String("directory", "/usr/local/rct", "Path to the folder storing RCT experiment data")
templateDirectory := flag.String("template-directory", "/usr/local/rct/server/templates", "Path to the folder storing RCT server templates (this file -> server/templates)")
staticDirectory := flag.String("static-directory", "/usr/local/rct/server/static", "Path to the folder storing RCT server templates (this file -> server/static)")
flag.Parse()
if len(flag.Args()) > 0 {
log.Fatalf("Usage: server [-directory DIRECTORY] ")
}
h := new(RegexpHandler)
renderRoute, err := regexp.Compile("/experiments/.*/iterations/.+/.+/render")
if err != nil {
log.Fatal(err)
}
iterationRoute, err := regexp.Compile("/experiments/.*/iterations/.+/.+")
if err != nil {
log.Fatal(err)
}
staticRoute, err := regexp.Compile("/static")
if err != nil {
log.Fatal(err)
}
defaultRoute, err := regexp.Compile("/")
if err != nil {
log.Fatal(err)
}
h.HandleFunc(renderRoute, renderHandler(*directory, *templateDirectory))
h.HandleFunc(iterationRoute, newRctHandler(*directory, *templateDirectory))
h.Handler(staticRoute, http.StripPrefix("/static", http.FileServer(http.Dir(*staticDirectory))))
h.Handler(defaultRoute, http.FileServer(http.Dir(*directory)))
allHandlers := jsonStripper(serverHeaderHandler(h))
log.Fatal(http.ListenAndServe(":8080", handlers.LoggingHandler(os.Stdout, allHandlers)))
}
示例2: Html2str
func Html2str(html string) string {
src := string(html)
//替换HTML的空白字符为空格
re := regexp.MustCompile(`\s`) //ns*r
src = re.ReplaceAllString(src, " ")
//将HTML标签全转换成小写
re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllStringFunc(src, strings.ToLower)
//去除STYLE
re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
src = re.ReplaceAllString(src, "")
//去除SCRIPT
re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
src = re.ReplaceAllString(src, "")
//去除所有尖括号内的HTML代码,并换成换行符
re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllString(src, "\n")
//去除连续的换行符
re, _ = regexp.Compile("\\s{2,}")
src = re.ReplaceAllString(src, "\n")
return strings.TrimSpace(src)
}
示例3: GetVersion
func GetVersion(t *testing.T) {
number_re, err := regexp.Compile("([0-9]+)")
if err != nil {
t.Fatal("test encountered an error")
}
dot_re, err := regexp.Compile("\\.")
if err != nil {
t.Fatal("test encountered an error")
}
version_from_function := getVersion()
numbers := number_re.FindAllString(version_from_function, -1)
dots := dot_re.FindAllString(version_from_function, -1)
if len(numbers) != 3 {
t.Fatal("too many version groups (should be MAJOR.MINOR.BUGFIX)")
}
reconstructed := []string{
numbers[0], dots[0],
numbers[1], dots[1],
numbers[2], dots[2],
}
check_string := strings.Join(reconstructed, "")
if version_from_function != check_string {
t.Fatal("unexpected results from version function")
}
}
示例4: firewall
// firewall checks that iptables does not have common firewall rules setup that would disrupt traffic
func firewall() error {
out, err := exec.Command("iptables", "-L", "INPUT").CombinedOutput()
if err != nil {
return printSuccess("Firewall Iptables Check %s: Could not run iptables", skipped)
}
inputRegex, err := regexp.Compile(iptablesInputRegexStr)
if err != nil {
// This should never happen and can only be fixed by changing the code
panic(err)
}
if inputRegex.Match(out) {
return printError("Firewall Iptables Check %s: Found INPUT rule matching %s", failed, iptablesInputRegexStr)
}
// Check GCE forward rules
out, err = exec.Command("iptables", "-L", "FORWARD").CombinedOutput()
if err != nil {
return printSuccess("Firewall Iptables Check %s: Could not run iptables", skipped)
}
forwardRegex, err := regexp.Compile(iptablesForwardRegexStr)
if err != nil {
// This should never happen and can only be fixed by changing the code
panic(err)
}
if forwardRegex.Match(out) {
return printError("Firewall Iptables Check %s: Found FORWARD rule matching %s", failed, iptablesInputRegexStr)
}
return printSuccess("Firewall Iptables Check %s", success)
}
示例5: CleanHtml
func CleanHtml(str string, depth int) string {
if depth > 0 {
//将HTML标签全转换成小写
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
str = re.ReplaceAllStringFunc(str, strings.ToLower)
}
if depth > 1 {
//去除STYLE
re, _ := regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
str = re.ReplaceAllString(str, "")
}
if depth > 2 {
//去除SCRIPT
re, _ := regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
str = re.ReplaceAllString(str, "")
}
if depth > 3 {
//去除所有尖括号内的HTML代码,并换成换行符
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
str = re.ReplaceAllString(str, "\n")
}
if depth > 4 {
//去除连续的换行符
re, _ := regexp.Compile("\\s{2,}")
str = re.ReplaceAllString(str, "\n")
}
return str
}
示例6: Initialize
// Initialize will initialize the munger
func (b *BlockPath) Initialize(config *github.Config, features *features.Features) error {
if len(b.Path) == 0 {
glog.Fatalf("--block-path-config is required with the block-path munger")
}
file, err := os.Open(b.Path)
if err != nil {
glog.Fatalf("Failed to load block-path config: %v", err)
}
defer file.Close()
c := &configBlockPath{}
if err := yaml.NewYAMLToJSONDecoder(file).Decode(c); err != nil {
glog.Fatalf("Failed to decode the block-path config: %v", err)
}
b.blockRegexp = []regexp.Regexp{}
for _, str := range c.BlockRegexp {
reg, err := regexp.Compile(str)
if err != nil {
return err
}
b.blockRegexp = append(b.blockRegexp, *reg)
}
b.doNotBlockRegexp = []regexp.Regexp{}
for _, str := range c.DoNotBlockRegexp {
reg, err := regexp.Compile(str)
if err != nil {
return err
}
b.doNotBlockRegexp = append(b.doNotBlockRegexp, *reg)
}
return nil
}
示例7: PrettySprint
func (i *Issue) PrettySprint() string {
sa := make([]string, 0)
sa = append(sa, fmt.Sprintln(i.String()))
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Jira URL: %s", i.Url())))
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Status: %s", i.Status)))
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Assignee: %s", i.Assignee)))
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Original time estimate: %s", PrettySeconds(int(i.OriginalEstimate)))))
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Time spent: %s", PrettySeconds(int(i.TimeSpent)))))
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Remaining time estimated: %s", PrettySeconds(int(i.RemainingEstimate)))))
r, _ := regexp.Compile("[*]([^*]*)[*]")
splitdesc := strings.Split(i.Description, "\n")
for k, v := range splitdesc {
splitdesc[k] = r.ReplaceAllString(v, "\x1b[1m$1\x1b[22m")
}
desc := strings.Join(splitdesc, "\n")
desc = strings.Replace(desc, "{color:red}", "\x1b[31m", -1)
desc = strings.Replace(desc, "{color}", "\x1b[39m", -1)
r2, _ := regexp.Compile("(?s)[{]quote[}](.*)[{]quote[}]")
desc = r2.ReplaceAllString(desc, "\x1b[51$1\x1b[54")
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Description: %s", desc)))
if len(i.Files) > 0 {
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Files: \n%v", i.Files)))
}
if len(i.Comments) > 0 {
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Comments: \n%v", i.Comments)))
}
if len(i.TimeLog) > 0 {
sa = append(sa, fmt.Sprintln(fmt.Sprintf("Worklog: \n%v", i.TimeLog)))
}
return strings.Join(sa, "\n")
}
示例8: matchRegularExpressions
func matchRegularExpressions(reader io.Reader, config HTTPProbe) bool {
body, err := ioutil.ReadAll(reader)
if err != nil {
log.Errorf("Error reading HTTP body: %s", err)
return false
}
for _, expression := range config.FailIfMatchesRegexp {
re, err := regexp.Compile(expression)
if err != nil {
log.Errorf("Could not compile expression %q as regular expression: %s", expression, err)
return false
}
if re.Match(body) {
return false
}
}
for _, expression := range config.FailIfNotMatchesRegexp {
re, err := regexp.Compile(expression)
if err != nil {
log.Errorf("Could not compile expression %q as regular expression: %s", expression, err)
return false
}
if !re.Match(body) {
return false
}
}
return true
}
示例9: webclient_render
/*-----------------------------------------------------------------------
| Function: WebClient Render
| Description: Takes screenshot of website, can be substituted for any Tool
| Inputs:
| final_url - URL of the Webpage After Redirects
| Returns:
| filename - Filename of the Rendered Picture
*-----------------------------------------------------------------------*/
func webclient_render(time_sec int, final_url string) (filename string) {
// Strip Prefix on URL
regex, _ := regexp.Compile("://")
filename = regex.ReplaceAllString(final_url, "_")
// Change (. or /) to _
dot_regex, _ := regexp.Compile("\\.|/!$|:|/")
filename = dot_regex.ReplaceAllString(filename, "_")
// Add .PNG File Extension and Remove / to prevent directories
end_regex, _ := regexp.Compile("_?/?$")
filename = end_regex.ReplaceAllString(filename, ".png")
file_path := "images/" + filename
timeout := strconv.Itoa(time_sec)
// Execute Render Command
cmd := exec.Command("/usr/bin/python", "webkit2png/scripts.py", final_url, "-o", file_path, "-t", timeout)
error := cmd.Start()
if error != nil {
log.Fatal(error)
}
cmd.Wait()
fmt.Println("[+] Rendered:", filename)
return
}
示例10: makeTargets
func makeTargets(lines []string, startIndex int) []Target {
startRE, _ := regexp.Compile(`TargetStart, ([\d\.]+), (\d+), (\d+), (\d+)`)
targetEndRE, _ := regexp.Compile(`(TargetHit|FriendHit|TargetTimeout), ([\d\.]+), ([\d\.]+), ([\d\.]+), (\d+)(, friend)?`)
// get info on the three targets
targetObjs := make([]Target, 3, 3)
targetNumber := 0
for i := startIndex; targetNumber < 3 && i < len(lines); i++ {
// set target info
if match := startRE.FindStringSubmatch(lines[i]); match != nil {
targetObjs[targetNumber].startTime, _ = strconv.ParseFloat(match[1], 64)
targetObjs[targetNumber].ID, _ = strconv.ParseInt(match[4], 10, 64)
targetObjs[targetNumber].startX, _ = strconv.ParseFloat(match[2], 64)
targetObjs[targetNumber].startY, _ = strconv.ParseFloat(match[3], 64)
// find end of target
for j := i; j < len(lines); j++ {
if match := targetEndRE.FindStringSubmatch(lines[j]); match != nil {
if id, _ := strconv.ParseInt(match[5], 10, 32); id == targetObjs[targetNumber].ID {
targetObjs[targetNumber].endX, _ = strconv.ParseFloat(match[3], 64)
targetObjs[targetNumber].endY, _ = strconv.ParseFloat(match[4], 64)
targetObjs[targetNumber].endTime, _ = strconv.ParseFloat(match[2], 64)
friend := strings.HasPrefix(match[1], "Friend") || len(match[6]) > 0
targetObjs[targetNumber].enemy = !friend
break
}
}
}
targetNumber++
}
}
return targetObjs
}
示例11: Subscribe
func (w *resourceWrapper) Subscribe(sub *messages.Subscription) *messages.Error {
compiledVerbReg, err := regexp.Compile(sub.VerbReg)
if err != nil {
return &messages.Error{
Message: err.Error(),
Code: messages.ErrorCodeRegexp,
}
}
compiledMethReg, err := regexp.Compile(sub.MethReg)
if err != nil {
return &messages.Error{
Message: err.Error(),
Code: messages.ErrorCodeRegexp,
}
}
compiledEventTypeReg, err := regexp.Compile(sub.EventTypeReg)
if err != nil {
return &messages.Error{
Message: err.Error(),
Code: messages.ErrorCodeRegexp,
}
}
w.router.RegisterSubscriber(w.resource, &subWrapper{
sub: sub,
compiledVerbReg: compiledVerbReg,
compiledMethReg: compiledMethReg,
compiledEventTypeReg: compiledEventTypeReg,
})
return nil
}
示例12: Fixurl
func Fixurl(current_url, url string) string {
re1, _ := regexp.Compile("http[s]?://[^/]+")
destrooturl := re1.FindString(current_url)
//当url为://wahaha/xiaoxixi/tupian.png
if strings.HasPrefix(url, "//") {
url = "http:" + url
} else if strings.HasPrefix(url, "/") {
// re1,_ := regexp.Compile("http[s]?://[^/]+")
// destrooturl := re1.FindString(current_url)
url = destrooturl + url
}
//当url为:"../wahaha/xiaoxixi/tupian.png"、"./wahaha/xiaoxixi/tupian.png"、"wahaha/xiaoxixi/tupian.png"
if !strings.HasPrefix(url, "/") && !strings.HasPrefix(url, "http") && !strings.HasPrefix(url, "https") {
// current_url = strings.TrimSuffix(current_url, "/")
if destrooturl == current_url {
url = current_url + "/" + url
} else {
re2, _ := regexp.Compile("[^/]+?$")
url = re2.ReplaceAllString(current_url, "") + url
}
}
return url
}
示例13: TestParseListSeries
func (self *QueryParserSuite) TestParseListSeries(c *C) {
queries, err := ParseQuery("list series")
c.Assert(err, IsNil)
c.Assert(queries, HasLen, 1)
c.Assert(queries[0].IsListQuery(), Equals, true)
listSeriesQuery := queries[0].GetListSeriesQuery()
c.Assert(listSeriesQuery, NotNil)
c.Assert(listSeriesQuery.HasRegex(), Equals, false)
c.Assert(listSeriesQuery.IncludeSpaces, Equals, false)
// test the case sensitive and case insensitive list series
for i := 0; i < 2; i++ {
query := "list series /^foo.*/"
if i == 1 {
query += "i"
}
queries, err = ParseQuery(query)
c.Assert(err, IsNil)
c.Assert(queries, HasLen, 1)
c.Assert(queries[0].IsListSeriesQuery(), Equals, true)
listSeriesQuery = queries[0].GetListSeriesQuery()
c.Assert(listSeriesQuery, NotNil)
c.Assert(listSeriesQuery.HasRegex(), Equals, true)
var regularExpression *regexp.Regexp
if i == 1 {
regularExpression, _ = regexp.Compile("(?i)^foo.*")
} else {
regularExpression, _ = regexp.Compile("^foo.*")
}
c.Assert(listSeriesQuery.GetRegex(), DeepEquals, regularExpression)
c.Assert(listSeriesQuery.IncludeSpaces, Equals, false)
}
}
示例14: etotal
func etotal(sid string, client *http.Client,
initiateData *dataproviders.InitiateData, pv *dataproviders.PvData, resp *[]byte) error {
reg, err := regexp.Compile(etotalRegEx)
if err != nil {
log.Fail(err.Error())
return err
}
foundPart1 := reg.Find(*resp)
if len(foundPart1) < 6 {
err := fmt.Errorf("Could not find etotal in response from inverter")
return err
}
reg, err = regexp.Compile(numberRegEx)
if err != nil {
log.Fail(err.Error())
}
foundPart2 := reg.Find(foundPart1)
if len(foundPart2) < 2 {
err = fmt.Errorf("Could not find etotal in response from inverter")
return err
}
etotal := string(foundPart2[1:])
log.Debugf("Current etotal is %s", etotal)
etotalfloat, err := strconv.ParseFloat(etotal, 64)
pv.EnergyTotal = float32(etotalfloat)
return nil
}
示例15: compareMatch
func compareMatch(got, expect interface{}) (int, error) {
switch got := got.(type) {
case []byte:
switch expect := expect.(type) {
case string:
matcher, err := regexp.Compile(expect)
if err != nil {
return 0, err
}
return match(got, matcher)
case *regexp.Regexp:
return match(got, expect)
}
default:
if got, err := toString(got); err == nil {
switch expect := expect.(type) {
case string:
matcher, err := regexp.Compile(expect)
if err != nil {
return 0, err
}
return matchString(got, matcher)
case *regexp.Regexp:
return matchString(got, expect)
}
} else {
return 0, err
}
}
return 0, errInvalid
}