`

ruby 的回溯 正则表达式

阅读更多
password =~ /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,40}$/
#(?=.*\d) any thing with a number
#at least one number one up case one down case





^ - Matches beginning of line
$ - Matches end of line
\A - Matches beginning of string.
\Z - Matches end of string. If string ends with a newline, it matches just before newline
\z - Matches end of string
\G - Matches point where last match finished
\b - Matches word boundaries when outside brackets; backspace (0x08) when inside brackets
\B - Matches non-word boundaries
(?=pat) - Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text
(?!pat) - Negative lookahead assertion: ensures that the following characters do not match pat, but doesn't include those characters in the matched text
(?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text
(?<!pat) - Negative lookbehind assertion: ensures that the preceding characters do not match pat, but doesn't include those characters in the matched text
# If a pattern isn't anchored it can begin at any point in the string

/real/.match("surrealist") #=> #<MatchData "real">
# Anchoring the pattern to the beginning of the string forces the
# match to start there. 'real' doesn't occur at the beginning of the
# string, so now the match fails
/\Areal/.match("surrealist") #=> nil
# The match below fails because although 'Demand' contains 'and', the
pattern does not occur at a word boundary.
/\band/.match("Demand")
# Whereas in the following example 'and' has been anchored to a
# non-word boundary so instead of matching the first 'and' it matches
# from the fourth letter of 'demand' instead
/\Band.+/.match("Supply and demand curve") #=> #<MatchData "and curve">
# The pattern below uses positive lookahead and positive lookbehind to
# match text appearing in <b></b> tags without including the tags in the
# match
/(?<=<b>)\w+(?=<\/b>)/.match("Fortune favours the <b>bold</b>")
    #=> #<MatchData "bold">

http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm
分享到:
评论
2 楼 ruby_windy 2012-03-08  
1 楼 ruby_windy 2012-03-08  
在线正则表达式验证:

http://rubular.com/

相关推荐

    正则表达式经典实例

    4. **高级特性详解**:深入探讨了更复杂的正则表达式特性,如回溯控制、条件匹配、命名捕获组等,并通过具体例子展示如何利用这些特性编写更高效、更灵活的正则表达式。 5. **解决常见问题**:针对性能不佳、误报、...

    精通正则表达式_第三版(英文版)

    最后,书中会讨论如何在各种编程语言中使用正则表达式,包括Perl、Java、JavaScript、Python和Ruby等。每种语言都有其特有的API和实现细节,理解这些差异对于实际开发至关重要。 总之,《精通正则表达式》第三版是...

    精通正则表达式~~~

    精通正则表达式第三版 搜集于网络 前言..........I 第1章:正则表达式入门.... 1 解决实际问题... 2 作为编程语言的正则表达式... 4 以文件名做类比... 4 以语言做类比... 5 正则表达式的知识框架... 6 对于...

    精通正则表达式英文原版-第3版

    正则表达式作为一个强大的工具,在处理文本和数据时拥有极高的效率和灵活性,被广泛应用于多种编程语言和流行工具中,比如Perl、Python、Ruby、Java、***、C#(以及任何使用.NET框架的语言)、PHP和MySQL。...

    正则表达式学习手册

    2. **通过正则表达式语言本身:** Perl、Ruby、JavaScript等语言直接支持正则表达式的语法,允许用户直接在代码中使用正则表达式进行文本处理。 对于想要深入了解并熟练掌握正则表达式的开发者来说,建议选择一种...

    精通正则表达式-第三版-简体中文版

    1. **回溯与效率**:正则表达式中的回溯可能导致性能问题,了解其原理可以帮助优化表达式。 2. **模式匹配算法**:了解DFA(确定有限状态自动机)和NFA(非确定有限状态自动机)的区别,有助于理解不同语言中的正则...

    正则表达式,验证资料

    3. Rubular:专为Ruby语言设计的正则表达式测试平台。 通过深入学习这些概念和技巧,你可以灵活运用正则表达式解决各种文本处理问题,提高工作效率。正则表达式虽然初学者可能觉得复杂,但随着实践和理解的加深,你...

    【内部培训】正则表达式

    - **Perl兼容**:Perl语言对正则表达式的支持最全面,其他语言如Python、Ruby等很大程度上兼容Perl的正则语法。 - **JavaScript正则**:JavaScript的正则表达式有些特殊,如它不支持条件表达式和嵌套反向引用。 -...

    循序渐进掌握递归正则表达式【推荐】

    文章中提到使用Ruby来介绍递归正则表达式,但其实这些知识可以广泛应用于任何支持递归正则表达式的编程语言。例如,虽然JavaScript原生不支持递归正则表达式,但借助第三方库或者正则表达式工具,我们可以实现类似的...

    ruby 正则表达式详解及示例代码

    因此,在编写正则表达式时,要尽量优化正则表达式结构,避免不必要的回溯和重复计算。 总之,Ruby正则表达式是文本处理的强大工具,提供了非常丰富的语法和灵活的匹配模式。掌握其使用方法对于任何使用Ruby进行开发...

    list_matcher:Ruby正则表达式生成器

    用于从字符串列表创建紧凑的,非回溯的正则表达式。 安装 将此行添加到您的应用程序的Gemfile中: gem 'list_matcher' 然后执行: $ bundle 或将其自己安装为: $ gem install list_matcher 概要 require '...

    正则表达式匹配解析过程探讨分析(正则表达式匹配原理)

    NFA引擎则采用“贪婪的”匹配回溯算法,它会按指定顺序测试正则表达式的所有可能扩展,并接受第一个匹配项。由于NFA在执行匹配时会考虑多种可能性,并且可以多次访问同一状态,所以在最坏情况下,它的执行速度可能会...

    Regular Expressions

    正则表达式广泛应用于多种编程语言和应用程序中,包括但不限于Java、JavaScript、MySQL、C#、Perl、PHP、Python、Ruby、VB、VBScript和XML。 正则表达式的基本组成部分包括元字符(metacharacters)和普通字符...

Global site tag (gtag.js) - Google Analytics