- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 TutorialsPoint 上查看一段代码,从那以后有些事情一直困扰着我……看看这段代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
public static void main( String args[] ){
// String to be scanned to find the pattern.
String line = "This order was placed for QT3000! OK?";
String pattern = "(.*)(\\d+)(.*)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
while(m.find( )) {
System.out.println("Found value: " + m.group(1));
System.out.println("Found value: " + m.group(2));
System.out.println("Found value: " + m.group(3));
}
}
}
此代码成功打印:
Found value: This was placed for QT300
Found value: 0
Found value: ! OK?
但是根据正则表达式 "(.*)(\\d+)(.*)"
,为什么它不返回其他可能的结果,例如:
Found value: This was placed for QT30
Found value: 00
Found value: ! OK?
或
Found value: This was placed for QT
Found value: 3000
Found value: ! OK?
如果此代码不适合这样做,那么我如何编写一个可以找到所有可能匹配项的代码?
最佳答案
这是因为 greediness的 *
然后是backtracking .
字符串:
This order was placed for QT3000! OK?
正则表达式:
(.*)(\\d+)(.*)
我们都知道.*
是贪心的,尽可能匹配所有字符。所以第一个.*
匹配所有字符直到最后一个字符 ?
然后它回溯以提供匹配。我们正则表达式中的下一个模式是 \d+
,所以它回溯到一个数字。一旦它找到一个数字,\d+
匹配该数字,因为此处满足条件( \d+
匹配一个或多个数字)。现在第一个(.*)
捕获 This order was placed for QT300
和以下 (\\d+)
捕获数字 0
位于 !
之前符号。
现在下一个模式(.*)
捕获所有剩余字符 !<space>OK?
. m.group(1)
指的是存在于组索引 1 和 m.group(2)
中的字符指的是索引 2,就这样继续下去。
查看演示 here .
得到你想要的输出。
String line = "This order was placed for QT3000! OK?";
String pattern = "(.*)(\\d{2})(.*)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
while(m.find( )) {
System.out.println("Found value: " + m.group(1));
System.out.println("Found value: " + m.group(2));
System.out.println("Found value: " + m.group(3));
}
输出:
Found value: This order was placed for QT30
Found value: 00
Found value: ! OK?
(.*)(\\d{2})
, 回溯最多两位数以提供匹配。
把你的模式改成这个,
String pattern = "(.*?)(\\d+)(.*)";
要得到这样的输出,
Found value: This order was placed for QT
Found value: 3000
Found value: ! OK?
?
在*
之后强制 *
进行非贪婪匹配。
使用额外的捕获组从单个程序中获取输出。
String line = "This order was placed for QT3000! OK?";
String pattern = "((.*?)(\\d{2}))(?:(\\d{2})(.*))";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while(m.find( )) {
System.out.println("Found value: " + m.group(1));
System.out.println("Found value: " + m.group(4));
System.out.println("Found value: " + m.group(5));
System.out.println("Found value: " + m.group(2));
System.out.println("Found value: " + m.group(3) + m.group(4));
System.out.println("Found value: " + m.group(5));
}
输出:
Found value: This order was placed for QT30
Found value: 00
Found value: ! OK?
Found value: This order was placed for QT
Found value: 3000
Found value: ! OK?
关于Java Regular Expression Matcher 没有找到所有可能的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28038364/
我正在使用 Swift 开发一个项目,当时我正在使用 Storyboard为 iPad 和 iPhone 创建 UI,因此我同时需要更改 Storyboard的大小(我使用的是大小类)。在我为 iPh
此示例数据由 Web 服务返回 200,6, "California, USA" 我想使用 split(",") 拆分它们并尝试使用简单的代码查看结果。 String loc = "200,6,\"C
我正在尝试创建正则表达式,以便我可以使用 LucidWorks 对我网站上的某些 URL 进行爬网和索引。 示例网址:http://www.example.com/reviews/assassins-
在检查给定语言是否正常时,我有点困惑。 假设我们必须检查是否: L. The language accepting even number of 0's in regular or not? 我们知道
我真的是新手,所以我为这里的笨拙而道歉。 构造识别以下语言的Deterministic Finite Automaton DFA: L= { w : w has at least two a's an
Pumping Lemma被用来证明一种不规则的语言。但是语言如何 证明是正常的?尤其是, Let L be a language. Define half(L) to be { x | for s
我刚刚开始阅读有关泵送引理的内容,并且知道如何进行一些证明,主要是通过反证法。我似乎找不到答案,只是这个特殊问题。我不知道如何开始。我可以假设必须有一个泵浦长度 P 并且对于 L 的所有 w 元素,L
如果语言 L1,...,Ln 是正则的,那么它们的并集也是正则的吗? 我们知道两种正则语言的并集是正则语言。如何证明多个正则语言的并集也是正则的? 最佳答案 您可以使用归纳法。这是一个非常非常生锈的证
如果对于其语言中的任何字符串 w,PDA(Pushdown Automaton)最多转动其堆栈的方向 k 次,则称其为 k 转。众所周知,语言 L 是线性的当且仅当被 1 圈 PDA 接受。现在,常规
如果语言 L1,...,Ln 是正则的,那么它们的并集也是正则的吗? 我们知道两种正则语言的并集是正则语言。如何证明多个正则语言的并集也是正则的? 最佳答案 您可以使用归纳法。这是一个非常非常生锈的证
给 R 常规语言。 下面的语言是否也是正则的: Comp(R) = { u | u is NOT a sub-word of a word in R } 看起来 Comp(R) 中没有单词,因为 R
设 L1 和 L2 是字母表 {a,b} 上的常规语言。我们定义语言 L3 如下: L3 = {pqr | pr ∈ L1, q ∈ L2} L3 是通过将来自L2 的字符串插入来自L1 的字符串而获
我们都知道(a + b)*是仅包含符号的常规语言 a和 b . 但是(a + b)*是一个无限长的字符串,它是有规律的,因为我们可以建立一个有限自动机,所以它应该是有限的。 任何人都可以解释一下吗?
如何计算常规语言的最小抽水长度。例如,如果我有 0001*,那么最小抽气长度应该是 4,即 000 无法抽气。为什么会这样? 最佳答案 它将小于或等于该语言的最小 DFA 中的状态数减去一。因此,将正
我有以下代码可以执行我想要的操作,从该命令的结果中检索包名称: 命令: dpkg --get-selections | grep amule 要分析的字符串: string = 'amule\t\t\
1、什么是正则表达式? 简单的说:正则表达式(Regular Expression)是一种处理字符串匹配的语言; 正则表达式描述了一种字符串匹配的模式,可以用来检查一个字符串是否含有某种子串,
前言 正则表达式是烦琐的,但是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感。只要认真去阅读这些资料,加上应用的时候进行一定的参考,掌握正则表达式不是问题。 1. 引子
给定 L1 上下文无关的非正则语言。 给定 L2 常规语言。 L1 U L2 =常规语言有可能吗? 另外,L1*L2 = 常规语言有可能吗? 我认为第二个是不可能的。但我不确定。 如果上述陈述之一(或
我需要解决抽奖引理问题。 L = { {a,b,c}* | #a(L) N个字母。 pigeon principle告诉我们必须存在一个达到2次的状态,因此在该状态下将存在一个循环。 用您的符号,您
我知道 n > 0 的 anbn 不是抽引引理的规则,但我可以想象 a*b*是常规的,因为 a,b 不必是相同的长度。有没有证据证明它是正常的? 最佳答案 回答你的问题: imagine a*b* t
我是一名优秀的程序员,十分优秀!