`
mryufeng
  • 浏览: 977664 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

未公开的Lua Frontier Pattern %f 比较实用

    博客分类:
  • Lua
阅读更多
参见: http://lua-users.org/wiki/FrontierPattern

Frontier Pattern

lua-users home
wiki

The "frontier" expression pattern %f is undocumented in the standard Lua references (for reasons why see LuaList:2006-12/msg00536.html).

I would like to present here the usefulness of it, in an attempt to show how it can be used, and why it should be retained.

Let's consider a fairly straightforward task: to find all words in upper-case in a string.

First attempt: %u+

    string.gsub ("the QUICK brown fox", "%u+", print)

    QUICK

That looks OK, found a word in all caps. But look at this:

    string.gsub ("the QUICK BROwn fox", "%u+", print)

    QUICK
    BRO

We also found a word which was partially capitalised.

Second attempt: %u+%A

    string.gsub ("the QUICK BROwn fox", "%u+%A", print)

    QUICK

The detection of non-letters correctly excluded the partially capitalised word. But wait! How about this:

    string.gsub ("the QUICK brOWN fox", "%u+%A", print)

    QUICK
    OWN

We also have a second problem:

    string.gsub ("the QUICK. brown fox", "%u+%A", print)

    QUICK.

The punctuation after the word is now part of the captured string, which is not wanted.

Third attempt: %A%u+%A

    string.gsub ("the QUICK brOWN FOx jumps", "%A%u+%A", print)

     QUICK

This correctly excludes the two partially capitalised words, but still leaves the punctuation in, like this:

    string.gsub ("the (QUICK) brOWN FOx jumps", "%A%u+%A", print)

    (QUICK)

Also, there is another problem, apart from capturing the non-letters at the sides. Look at this:

    string.gsub ("THE (QUICK) brOWN FOx JUMPS", "%A%u+%A", print)

    (QUICK)

The correctly capitalised words at the start and end of the string are not detected.

The solution: The Frontier pattern: %f

    string.gsub ("THE (QUICK) brOWN FOx JUMPS", "%f[%a]%u+%f[%A]", print)

    THE
    QUICK
    JUMPS

The frontier pattern %f followed by a set detects the transition from "not in set" to "in set". The source string boundary qualifies as "not in set" so it also matches the word at the very start of the string to be matched.

The second frontier pattern is also matched at the end of the string, so our final word is also captured.

Alternatives without the frontier pattern

Without the frontier pattern, one might resort to things like this:

    s = "THE (QUICK) brOWN FOx JUMPS"
    s = "\0" .. s:gsub("(%A)(%u)", "%1\0%2")
                 :gsub("(%u)(%A)", "%1\0%2") .. "\0"
    s = s:gsub("%z(%u+)%z", print)

This page brought to you by NickGammon. FindPage · RecentChanges · preferences
edit · history
Last edited July 7, 2007 7:17 pm GMT (diff)


看下代码lstrlib.c:

        case 'f': {  /* frontier? */
          const char *ep; char previous;
          p += 2;
          if (*p != '[')
            luaL_error(ms->L, "missing " LUA_QL("[") " after "
                               LUA_QL("%%f") " in pattern");
          ep = classend(ms, p);  /* points to what is next */
          previous = (s == ms->src_init) ? '\0' : *(s-1);
          if (matchbracketclass(uchar(previous), p, ep-1) ||
             !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
          p=ep; goto init;  /* else return match(ms, s, ep); */
        }

就知道如何用了。
3
1
分享到:
评论

相关推荐

    frontier与stata操作案例比较.pdf

    《Frontier与Stata操作案例比较》 在经济学与统计学的研究中,数据分析软件的选择往往影响着研究结果的准确性和可解释性。Frontier和Stata作为两款常用的数据分析工具,各自有着独特的功能和适用场景。本文将通过...

    关于两个模型面板数据和截面数据的操作.rar_Frontier4.1_frontier4.1操作_frontier4.1教程_截

    在Frontier4.1中,分析截面数据通常用于比较不同单位(如企业或国家)在特定时期的效率差异。 3. **Frontier4.1操作**:该软件提供了用户友好的图形用户界面,允许用户导入数据、设定模型参数、运行估计和进行诊断...

    FRONTIER4.1

    "FRONTIER 4.1"是一款专门用于随机前沿分析(Stochastic Frontier Analysis, SFA)的软件工具,常被经济学家和数据分析师用于衡量生产效率、服务效率或技术效率。这款软件可以帮助用户处理含有误差项的数据,从而...

    FRONTIER4.1--随机前沿分析

    **随机前沿分析(Stochastic Frontier Analysis, SFA)** 随机前沿分析是一种在经济学和管理科学中广泛应用的计量方法,主要用于估计生产效率、服务效率或技术效率。它结合了随机误差和前沿面模型,解决了传统效率...

    DEA FRONTIER 4.1操作说明及软件

    使用Frontier Analyst可以增加生产效能的获利,它提供您了解如何管理您的营业单位和改进效能,Frontier Analyst使用一种被称为 “数据包络分析” (Data Envelopment Analysis, DEA) 的方法,有时被称为比较性能的...

    deap2.1+Frontier41

    DEAP(Distributed Evolutionary Algorithms in Python)是一个用于开发多目标优化算法的Python库,而Frontier41则是一款基于数据包络分析(DEA)的软件,常用于效率评估和决策支持。DEAP提供了模块化的框架,允许...

    jQuery Frontier Calendar 1.3.2

    google code上的一个开源项目源码、实例...当在一个日期单元格中有比较多的待办事项时,将出现一个更多的链接,点击这个链接会弹一个jQuery模式对话框来显示所有的事务。这个日历还支持通过CSS文件改变整个日历的背景。

    iOS-tech-frontier.zip

    这个项目的主旨在于推动iOS开发者的技术边界,提供最新、最实用的iOS开发实践。 开源项目的优势在于,它们不仅提供了可复用的代码,还让我们有机会深入理解优秀代码的设计思想和实现方式。"iOS-tech-frontier"项目...

    FRONTIER

    "FRONTIER"是一个可能与字体设计相关的主题。在IT领域,字体是计算机显示和打印文本时所用的样式,包括字母、数字和符号的形状。"FRONTIER"可能是某种特定字体的名称,也可能是一个字体库或者字体设计项目的代号。在...

    使用Frontier4.1做随机前沿引力模型的相关资料和操作步骤及假设检验

    使用Frontier4.1做随机前沿引力模型的相关资料和操作步骤及假设检验 用随机前沿模型测算贸易潜力,用的是BC95模型。在整个论文写作过程中,收集了许多关于随机前沿引力模型的教学方法,从一开始对随机前沿引力模型的...

    Frontier4.1软件用法 ppt 教学视频集合.rar

    Frontier是一款专门用于完成随机前沿分析的软件,它可以用最大似然法估计随机前沿成本模型(Stochastic Frontier Cost Model)和随机前沿生产模型(Stochastic Frontier Production Model)。本文主要介绍SFA的专用分析...

    Frontier Analyst DEA分析软件

    虽然试用版本只能处理12个样本,但经亲自测试:先打开软件提供的案例(超过12个样本的案例如Example Project.fa4),进行分析后将其关闭,然后再导入自己的数据,就可以分析多于12个的样本。

    Production Possibility Frontier

    生产可能性边界(Production Possibility Frontier,PPF)是经济学中的一个关键概念,它描绘了在所有可用资源被高效利用的情况下,两种或多种商品和服务能够同时生产的最大可能组合。PPF通常表现为从原点向外凹的...

    Frontier-and-Innovation-in-Future-Computing-and-Communications

    这是关于未来计算与通讯前沿的电子书,高清,最新版本,经典著作,英文版

    the sharpe ratio efficient frontier.pdf

    The Sharpe ratio (Sharpe 1992) is one industry standard for measuring the absolute risk adjusted performance of hedge funds. This function performs the testing of Sharpe ratio difference for two funds...

    frontier_exploration:ROS节点和Costmap 2D插件层用于前沿探索

    前沿探索在机器人领域是一项关键任务,它涉及到机器人在未知环境中自动发现并导航至未探索区域。在ROS(Robot Operating System)环境下,`frontier_exploration` 包提供了一个高效且灵活的解决方案,利用ROS节点和`...

    dlgitai minilab frontier

    - **W-1101/W-1123/W-1124/W-1210/W-1213:** 这些错误均提示未正确安装或缺失必要的部件(如胶片、胶片夹等),导致无法正常执行扫描或打印任务。解决方案是按照设备说明书要求正确安装所需部件。 - **W-1121/W-...

    太空前沿游戏模板:Space Frontier complete game template 1.5.5

    《太空前沿》是由Finalboss游戏工作室带来的一款趣味十足且令人上瘾的物理火箭游戏模板。在游戏中,您将控制一枚向上飞行的火箭,并需要管理其燃料消耗,以使其达到尽可能高的高度。 特色: 这是一款完整的、即将...

Global site tag (gtag.js) - Google Analytics