有人能以一种可以理解的方式解释这两个术语吗?
我发现这个关于正则表达式的优秀教程,虽然我直观地理解"贪婪","不情愿"和"占有欲"量词的作用,但我的理解似乎存在严重漏洞.
具体来说,在以下示例中:
Enter your regex: .*foo // greedy quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.
Enter your regex: .*?foo // reluctant quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfoo" starting at index 0 and ending at index 4.
I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.
Enter your regex: .*+foo // possessive quantifier
Enter …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用正则表达式将字符串分成两部分.字符串格式如下:
text to extract<number>
Run Code Online (Sandbox Code Playgroud)
我一直在使用(.*?)<,<(.*?)>哪个工作正常,但在阅读了一点regex之后,我才开始想知道为什么我需要?表达式中的.我通过这个网站找到它们之后才这样做,所以我不确定它们之间的区别.
在java中,我试图检测表单的字符串:可选的下划线,大写字母,然后是包含两个参数的大括号.就是这样的事情MAX{1,2} FUNC{3,7} _POW{9,10}
我决定推迟处理这些参数,所以我使用的正则表达式是:
_?[A-Z]+//{.*//}
Run Code Online (Sandbox Code Playgroud)
但是在尝试将其编译为Pattern对象时遇到以下错误:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 9
_?[A-Z]+//{.*//}
^
Run Code Online (Sandbox Code Playgroud)
谁知道问题是什么?
怎么会'.+?' 正规表达工作?.+部分是否匹配任何内容,以及?部分说它可以在那里吗?因此,例如,此正则表达式将匹配:
'cat'
''(即没有写,只是空字符串)
我有一个字符串
{! texthere }
Run Code Online (Sandbox Code Playgroud)
我希望在{!直到结束或你到达第一个}.所以,如果我有
{!text here} {!text here again} {!more text here.好家伙!
我希望["{{text here}","{{text here again}","{!more text here.哦,boy!"]
我认为这会奏效
{!*} ??
但上面的字符串会出现[{{{text here} {!text here again} {!more text here.哦,小男孩!"]
我仍然对正则表达式缺乏经验,所以我不明白为什么这不起作用.我认为它会匹配'{!' 然后是任意数量的字符,直到你得到一个可能不存在的括号(非贪婪).
我正在尝试理解以下代码:
Pattern.compile("(.*?):")
Run Code Online (Sandbox Code Playgroud)
我已经做了一些关于它可能意味着什么的研究,但我不太明白:
根据java文档,*意味着0次或更多次,而?意味着一次或根本不意味着.
另外,':'是什么意思?
谢谢