- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下正则表达式:
(.*)(?:([\+\-\*\/])(-?\d+(?:\.\d+)?))
目的是捕获(左表达式)(运算符)(右操作数)形式的数学表达式,例如1+2+3
将被捕获为 (1+2)(+)(3)
。它还将处理单个操作数,例如1+2
将被捕获为 (1)(+)(2)。
我遇到的问题是这个正则表达式在没有运算符的单个操作数上不匹配,例如5 应在第一个捕获组中匹配,而在第二个和第三个 (5)()() 中没有任何内容。如果我将最后一部分设为可选:
(.*)(?:([\+\-\*\/])(-?\d+(?:\.\d+)?))?
那么初始组将始终捕获整个表达式。有什么方法可以使第二部分可选,但让它优先于第一组完成的贪婪匹配?
最佳答案
此正则表达式将:
1+2
或 1+2+3
或 1+2+3+4
或 1+2+3+4...
原始正则表达式
请注意,这是 Java,您需要转义此正则表达式中的反斜杠。要转义它们,只需将所有 \
替换为 \\
。
^(?=(?:[-+*/^]?[-+]?\d+(?:[.]\d+)?)*$)([-+]?[0 -9.]+$|[-+]?[0-9.]+(?:[-+*/^][-+]?[0-9.]+)*(?=[-+*/^]))(?:([-+*/^])([-+]?[0-9.]+))?$
概述
在此表达式中,我首先验证字符串仅由运算 -+/*^
、可选符号 -+
以及整数或非整数组成。由于已经经过验证,表达式的其余部分可以简单地将数字引用为 [0-9.]+
,这提高了可读性。
捕获组
0 获取整个字符串1 获取整个字符串,但不包括最后一个操作,如果没有操作,则第 1 组将拥有整个字符串2 获取最后一次操作(如果存在)3 获取最后一次操作后的数字和符号
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
[-+*/^]? any character of: '-', '+', '*', '/',
'^' (optional (matching the most
amount possible))
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
[-+*/^] any character of: '-', '+', '*', '/',
'^'
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1
or more times (matching the most
amount possible))
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[-+*/^] any character of: '-', '+', '*', '/',
'^'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[-+*/^] any character of: '-', '+', '*', '/',
'^'
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1
or more times (matching the most
amount possible))
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
示例文本
1+2+-3
示例捕获组
[0] = 1+2+-3
[1] = 1+2
[2] = +
[3] = -3
示例文本
-3
示例捕获组
[0] = -3
[1] = -3
[2] =
[3] =
示例 Java 代码
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "source string to match with pattern";
Pattern re = Pattern.compile("^(?=(?:[-+*/^]?[-+]?\\d+(?:[.]\\d+)?)*$)([-+]?[0-9.]+$|[-+]?[0-9.]+(?:[-+*/^][-+]?[0-9.]+)*(?=[-+*/^]))(?:([-+*/^])([-+]?[0-9.]+))?$",Pattern.CASE_INSENSITIVE);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}
关于java - 正则表达式: match everything up to an optional capture group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36828548/
我一直在阅读 Captures这一段引起了我的兴趣: Inside a Signature, a Capture may be created by prefixing a sigilless par
我在 Java 中使用这个正则表达式: ^(Mon(?:.?|day)?)(?:[\.,])?$ (可以测试 here ) 我想捕获日期,后跟可选的 . 或 ,。如果是星期一,我想捕获 Monday
我正在 try catch 功能键 F1 到 F12 和 4 个箭头键以及主页、插入、删除、结束、向上翻页和向下翻页键。如何???? private void Form1_KeyPress(objec
没有capture="camera" input type="file" 的属性标签 in official w3.org documentation . 讽刺的是,我发现了这么多地方 capture
摘自Huon Wilson的Finding Closure in Rust: Capturing entirely by value is also strictly more general tha
所以我想这样做: public interface IFieldObject { public Comparable get(); } public interface IFieldCondi
我希望使用正则表达式将单词分成组(vowels, not_vowels, more_vowels),使用标记来确保每个单词以元音开头和结尾。 import re MARKER = "~" VOWELS
我在浏览 StackOverflow 时发现了 Szimek/Signature_Pad 以使用 Javascript 捕获电子/数字签名。 我研究过,但我仍然对如何将 DATA URI 捕获到变量中
我正在尝试关注 this example使用带有 remove_if 的 lambda。这是我的尝试: int flagId = _ChildToRemove->getId(); auto new_e
我无法捕获 在屏幕捕获区域内。 我想要一个定义的部分,其中包含要捕获的图像和内容。我们怎样才能做到这一点?帮助! 访问:https://stackblitz.com/edit/ngx-capture-
从 Perl 脚本调用外部程序时,Capture::Tiny 是否避免了使用 system() 时需要的磁盘 io?使用任何一种时,我都能获得基本相同的性能。一位同事正在使用我的代码并告诉我它正在敲打
作为数值方法研究的一部分,我正在编写一个函数来解决流值问题。这是该程序的“核心”,但它出现了一些奇怪的错误,这很奇怪,因为我在其他程序中使用了相同的代码段而没有出现任何错误。 void solve_
vector vec; //a auto foo = [&vec](){ //do something }; //b auto foo = [&v = vec](){ //do som
我正在使用 PyDev 对我的 Python 应用程序进行开发和单元测试。至于单元测试,除了没有内容被记录到日志框架之外,一切都很好。 PyDev 的“捕获的输出”没有捕获记录器。 我已经将记录的所有
你能帮我解决这个编译器错误吗? template static void ComputeGenericDropCount(function func) { T::ForEach([](T *w
第一次做泛型,我有点困惑。 我有以下内容: public interface GenericDao { /** * Retrieve an object that was previ
我正在尝试提取此代码中 dir_entry.path() 的值并想将其复制到 compFileName 中。问题是我一直收到错误“compFileName cannot be implicitly c
我正在使用在网上找到的 WebCam_Capture 代码通过 C# 访问网络摄像头。在一台只有一个视频源的计算机上,它就像一个魅力! (程序在启动时启动,找到网络摄像头并正常工作)。 虽然在一台有很
下面的代码 void CMainWindow::someMethod(const CLocationsCollection& parentItem) { auto f = [this, par
所以我打开了一个 youtube 页面,我可以在那里观看视频。 但是这个视频被用户下架了。我打开的页面仍然有视频,如果你再次访问(刷新)新页面没有。 由于我在浏览器选项卡 (chrome) 中加载了视
我是一名优秀的程序员,十分优秀!