当前位置: 首页>>代码示例>>Golang>>正文


Golang Regexp.Find方法代码示例

本文整理汇总了Golang中regexp.Regexp.Find方法的典型用法代码示例。如果您正苦于以下问题:Golang Regexp.Find方法的具体用法?Golang Regexp.Find怎么用?Golang Regexp.Find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在regexp.Regexp的用法示例。


在下文中一共展示了Regexp.Find方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: removePattern

// Removes the first parts of a string that match the pattern.
func removePattern(str string, pattern *regexp.Regexp) string {
	found := pattern.Find([]byte(str))

	if found != nil {
		return strings.Replace(str, string(found), "", 1)
	}

	return str
}
开发者ID:pablo-meier,项目名称:Ebooker,代码行数:10,代码来源:stringutils.go

示例2: find

func (r *FetchResult) find(re *regexp.Regexp) (numbers []int) {
	numbers = make([]int, LOTTO_SIZE)
	lotto := re.Find(r.body)
	noExp := regexp.MustCompile(`\d+`)
	noBytes := noExp.FindAll(lotto, -1)
	for i := 0; i < len(noBytes); i++ {
		if no, err := strconv.Atoi(string(noBytes[i])); err == nil {
			numbers[i] = no
		}
	}
	return
}
开发者ID:kpawlik,项目名称:luckynumbers,代码行数:12,代码来源:lotto.go

示例3: Search

// Search finds all lines matching regexp. context is the number of context
// lines to include before and after the match.
func (g *Grep) Search(r *regexp.Regexp, context int) []Match {
	matches := make([]Match, 0)

	for i, l := range g.lines {
		m := r.Find(l)
		if m == nil {
			continue
		}

		matches = append(matches, Match{
			LineNum:       i + 1,
			Match:         m,
			FullLine:      l,
			ContextBefore: g.context(i-context, i),
			ContextAfter:  g.context(i+1, i+1+context),
		})
	}

	return matches
}
开发者ID:prattmic,项目名称:codesearch,代码行数:22,代码来源:grep.go

示例4: Match

func (s *SimpleScanner) Match(pattern string) ([]byte, Scanner) {
	var regc *regexp.Regexp
	var err error

	if pattern[0] != '^' {
		panic("match patterns must begin with `^`")
	}

	regc = s.patterns[pattern]
	if regc == nil {
		if regc, err = regexp.Compile(pattern); err == nil {
			s.patterns[pattern] = regc
		} else {
			panic(err.Error())
		}
	}
	if token := regc.Find(s.buf[s.cursor:]); token != nil {
		s.cursor += len(token)
		return token, s
	}
	return nil, s
}
开发者ID:radiofreejohn,项目名称:goparsec,代码行数:22,代码来源:scanner.go

示例5: retrieve_urls

func retrieve_urls(uri string, urlRegexp *regexp.Regexp) []string {
	urls := []string{}
	resp, err := http.Get(uri)
	if err != nil {
		return urls
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	lines := strings.Split(string(body), "\n")
	for _, v := range lines {
		matched, _ := regexp.MatchString("<a href=\"http", v)
		if matched {
			matchedurl := string(urlRegexp.Find([]byte(v)))
			matchedurl = fixUrl(matchedurl)
			visited[matchedurl]++
			if visited[matchedurl] > 1 {
				continue
			} else {
				urls = append(urls, matchedurl)
			}
		}
	}
	return urls
}
开发者ID:yadavsms,项目名称:golang,代码行数:24,代码来源:crawler.go

示例6: performGeneralCleanup

func (c *CssCompressor) performGeneralCleanup() {
	// This function does a lot, ok?
	var sb bytes.Buffer
	var previousIndex int
	var re *regexp.Regexp

	// Remove the spaces before the things that should not have spaces before them.
	// But, be careful not to turn "p :link {...}" into "p:link{...}"
	// Swap out any pseudo-class colons with the token, and then swap back.
	c.Css = RegexFindReplace(c.Css,
		"(^|\\})(([^\\{:])+:)+([^\\{]*\\{)",
		func(groups []string) string {
			s := groups[0]
			s = strings.Replace(s, ":", "___YUICSSMIN_PSEUDOCLASSCOLON___", -1)
			s = strings.Replace(s, "\\\\", "\\\\\\\\", -1)
			s = strings.Replace(s, "\\$", "\\\\\\$", -1)
			return s
		})

	// Remove spaces before the things that should not have spaces before them.
	re, _ = regexp.Compile("\\s+([!{};:>+\\(\\)\\],])")
	c.Css = re.ReplaceAll(c.Css, []byte("$1"))
	// Restore spaces for !important
	c.Css = bytes.Replace(c.Css, []byte("!important"), []byte(" !important"), -1)
	// bring back the colon
	c.Css = bytes.Replace(c.Css, []byte("___YUICSSMIN_PSEUDOCLASSCOLON___"), []byte(":"), -1)

	// retain space for special IE6 cases
	c.Css = RegexFindReplace(c.Css,
		"(?i):first\\-(line|letter)(\\{|,)",
		func(groups []string) string {
			return strings.ToLower(":first-"+groups[1]) + " " + groups[2]
		})

	// no space after the end of a preserved comment
	c.Css = bytes.Replace(c.Css, []byte("*/ "), []byte("*/"), -1)

	// If there are multiple @charset directives, push them to the top of the file.
	c.Css = RegexFindReplace(c.Css,
		"(?i)^(.*)(@charset)( \"[^\"]*\";)",
		func(groups []string) string {
			return strings.ToLower(groups[2]) + groups[3] + groups[1]
		})

	// When all @charset are at the top, remove the second and after (as they are completely ignored).
	c.Css = RegexFindReplace(c.Css,
		"(?i)^((\\s*)(@charset)( [^;]+;\\s*))+",
		func(groups []string) string {
			return groups[2] + strings.ToLower(groups[3]) + groups[4]
		})

	// lowercase some popular @directives
	c.Css = RegexFindReplace(c.Css,
		"(?i)@(charset|font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframe|media|page|namespace)",
		func(groups []string) string {
			return "@" + strings.ToLower(groups[1])
		})

	// lowercase some more common pseudo-elements
	c.Css = RegexFindReplace(c.Css,
		"(?i):(active|after|before|checked|disabled|empty|enabled|first-(?:child|of-type)|focus|hover|last-(?:child|of-type)|link|only-(?:child|of-type)|root|:selection|target|visited)",
		func(groups []string) string {
			return ":" + strings.ToLower(groups[1])
		})

	// lowercase some more common functions
	c.Css = RegexFindReplace(c.Css,
		"(?i):(lang|not|nth-child|nth-last-child|nth-last-of-type|nth-of-type|(?:-(?:moz|webkit)-)?any)\\(",
		func(groups []string) string {
			return ":" + strings.ToLower(groups[1]) + "("
		})

	// lower case some common function that can be values
	// NOTE: rgb() isn't useful as we replace with #hex later, as well as and() is already done for us right after this
	c.Css = RegexFindReplace(c.Css,
		"(?i)([:,\\( ]\\s*)(attr|color-stop|from|rgba|to|url|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?(?:calc|max|min|(?:repeating-)?(?:linear|radial)-gradient)|-webkit-gradient)",
		func(groups []string) string {
			return groups[1] + strings.ToLower(groups[2])
		})

	// Put the space back in some cases, to support stuff like
	// @media screen and (-webkit-min-device-pixel-ratio:0){
	re, _ = regexp.Compile("(?i)\\band\\(")
	c.Css = re.ReplaceAll(c.Css, []byte("and ("))

	// Remove the spaces after the things that should not have spaces after them.
	re, _ = regexp.Compile("([!{}:;>+\\(\\[,])\\s+")
	c.Css = re.ReplaceAll(c.Css, []byte("$1"))

	// remove unnecessary semicolons
	re, _ = regexp.Compile(";+}")
	c.Css = re.ReplaceAll(c.Css, []byte("}"))

	// Replace 0(px,em,%) with 0.
	re, _ = regexp.Compile("(?i)(^|[^0-9])(?:0?\\.)?0(?:px|em|%|in|cm|mm|pc|pt|ex|deg|g?rad|m?s|k?hz)")
	c.Css = re.ReplaceAll(c.Css, []byte("${1}0"))

	// Replace 0 0 0 0; with 0.
	re, _ = regexp.Compile(":0 0 0 0(;|})")
	re2, _ := regexp.Compile(":0 0 0(;|})")
//.........这里部分代码省略.........
开发者ID:spiraleye,项目名称:BrownLegoCSS,代码行数:101,代码来源:BrownLegoCSS.go


注:本文中的regexp.Regexp.Find方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。