我试图[a-zA-Z]+用一个约束提取单词:一个单词必须包含至少一个小写字母和至少一个大写字母(在单词内的任何位置).示例:如果输入hello 123 worLD,则唯一匹配应该是worLD.
我尝试使用这样的正向前瞻:
echo "hello 123 worLD" | grep -oP "(?=.*[a-z])(?=.*[A-Z])[a-zA-Z]+"
hello
Run Code Online (Sandbox Code Playgroud)
这是不正确的:唯一的匹配是hello代替worLD.然后我尝试了这个:
echo "hello 123 worLD" | grep -oP "\K((?=.*[a-z])(?=.*[A-Z])[a-zA-Z]+)"
hello
worLD
Run Code Online (Sandbox Code Playgroud)
这仍然是错误的:hello不应该匹配.
为什么这个正则表达式在 Python 中有效,而在 Ruby 中无效:
/(?<!([0-1\b][0-9]|[2][0-3]))/
Run Code Online (Sandbox Code Playgroud)
很高兴听到解释以及如何在 Ruby 中解决它
使用整行代码进行编辑:
re.sub(r'(?<!([0-1\b][0-9]|[2][0-3])):(?!([0-5][0-9])((?i)(am)|(pm)|(a\.m)|(p\.m)|(a\.m\.)|(p\.m\.))?\b)' , ':\n' , s)
Run Code Online (Sandbox Code Playgroud)
基本上,我试图添加'\n'冒号而不是时间。
我有以下正则表达式,它在 chrome 中工作,但在 firefox 或 safari 中导致错误。我需要修改它以使其工作。有人可以帮助一个可怜的灵魂吗?提前致谢!
正则表达式: /(?=<tag>)(.*?)(?<=<\/tag>)/
基本上,我必须匹配之间的任何字符<tag>,</tag>并且需要保留两个标签。我使用这个表达式作为 array.split 的参数。
输入: "The quick brown <tag>fox</tag> jumps over the lazy <tag>dog</tag>"
操作: input.split(正则表达式)
输出: ["The quick brown ", "<tag>fox</tag>", " jumps over the lazy ", "<tag>dog</tag>"]
我正在阅读有关“嵌套结构的波浪号”的正则表达式文档。
关于使用的副业解释<?>是:
这里
<?>成功匹配了空字符串。
我以为我可以使用<?[]>它来代替它,但它没有这样做!
举个例子:
say so "" ~~ / <?> /;
say so "test" ~~ / <?> /;
say so "" ~~ / <?[]> /;
say so "test" ~~ / <?[]> /;
Run Code Online (Sandbox Code Playgroud)
响应:
True
True
False
False
Run Code Online (Sandbox Code Playgroud)
有人可以给我一个解释吗?
我正在尝试做一些简单的事情。在切片中u8,我想找到两个字符的出现"\r\n"。但是,我无法将该切片转换为Stringusingfrom_utf8因为切片之后的部分"\r\n"可能不是 utf-8 并且我尽可能不想使用from_utf8_unchecked. 所以我尝试了类似以下的方法。
fn find_crlf(text: &[u8]) -> Option<usize> {
let mut textiter = text.iter().peekable();
for (idx, &elem) in textiter.enumerate() {
if Some(&elem) == Some(&b'\r') {
if textiter.peek() == Some(&&b'\n') {
return Some(idx);
}
}
}
None
}
Run Code Online (Sandbox Code Playgroud)
我得到以下编译错误,这是可以理解的。但是,我不太确定如何去做。如果是str,那就是.find("\r\n")。
编译错误->
fn find_crlf(text: &[u8]) -> Option<usize> {
let mut textiter = text.iter().peekable();
for (idx, &elem) in textiter.enumerate() {
if Some(&elem) == Some(&b'\r') {
if textiter.peek() …Run Code Online (Sandbox Code Playgroud) 我坚持创建正确的正则表达式模式,该模式将拆分我的数据框列的内容,而不会让我失去任何元素。我必须使用包中的separate()函数,tidyr因为这是较长处理管道的一部分。由于我不想丢失字符串中的任何元素,因此我正在开发一个前瞻/后视表达式。
需要拆分的字符串可以遵循以下模式之一:
我想在每次元素更改时拆分,所以在字母和破折号之后。可以有一个或多个字母、一个或多个数字,但只能有一个破折号。只包含字母的字符串,不需要拆分。
这是我尝试过的:
library(tidyr)
myDat = data.frame(drugName = c("ab-1234", 'ab-1234', 'ab-1234',
'placebo', 'anotherdrug', 'andanother',
'xyz123', 'xyz123', 'placebo', 'another',
'omega-3', 'omega-3', 'another', 'placebo'))
drugColNames = paste0("X", 1:3)
# This pattern doesn't split strings that only consist of number and letters, e.g. "xyz123" is not split after the letters.
pat = '(?=-[0-9+])|(?<=[a-z+]-)'
# This pattern splits at all the right places, but the last group (the numbers), is separated and not …Run Code Online (Sandbox Code Playgroud) 注意:
观察到的行为是正确的,但起初可能令人惊讶;对我来说是这样,我认为对其他人也可能是这样——尽管对那些非常熟悉正则表达式引擎的人可能不是这样。
重复建议的重复项Regex lookahead、lookbehind 和 atomic groups包含有关环视断言的一般信息,但没有解决手头的具体误解,如下面的评论中更详细地讨论。
使用greedy,根据定义,在肯定的后视断言中的可变宽度子表达式可以表现出令人惊讶的行为。
为了方便起见,这些示例使用 PowerShell,但该行为通常适用于 .NET 正则表达式引擎:
这个命令按我直觉的预期工作:
# OK:
# The subexpression matches greedily from the start up to and
# including the last "_", and, by including the matched string ($&)
# in the replacement string, effectively inserts "|" there - and only there.
PS> 'a_b_c' -replace '^.+_', '$&|'
a_b_|c
Run Code Online (Sandbox Code Playgroud)
下面的命令,该命令使用正向后看断言,(?<=...)是看似等价-但不是: …
我正在编写一个模块,该模块将一些字符串替换为文本以提供给脚本语言。该语言的语法模糊不清,因此表达式以括号和空格分隔的符号为界,其中大多数以 '$' 开头。像这样的正则表达式似乎应该在适当的符号边界处给出匹配:
auto re_match_abc = std::regex{ "(?=.*[[:space:]()])\\$abc(?=[()[:space:]].*)" };
Run Code Online (Sandbox Code Playgroud)
但是在我的环境中(Visual C++ 2017, 15.9.19,targeting C++-17)它可以匹配前面没有合适边界的字符串:
std::cout << " $abc -> " << std::regex_replace(" $abc ", re_match_abc, "***") << std::endl;
std::cout << " ($abc) -> " << std::regex_replace("($abc)", re_match_abc, "***") << std::endl;
std::cout << "xyz$abc -> " << std::regex_replace("xyz$abc ", re_match_abc, "***") << std::endl;
std::cout << " $abcdef -> " << std::regex_replace(" $abcdef", re_match_abc, "***") << std::endl;
// Result from VC++ 2017:
//
// $abc -> ***
// ($abc) -> (***)
// …Run Code Online (Sandbox Code Playgroud) 我在 R 中有一个数据框。我想匹配并保留该行,如果
phrases_with_woman <- structure(list(phrase = c("woman get degree", "woman obtain justice",
"session woman vote for member", "woman have to end", "woman have no existence",
"woman lose right", "woman be much", "woman mix at dance", "woman vote as member",
"woman have power", "woman act only", "she be woman", "no committee woman passed vote")), row.names = c(NA,
-13L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我希望能够匹配除“she be Woman”之外的所有行。
这是我到目前为止的代码。我有一个积极的环视((?<=woman\\s)\\w+"),似乎在正确的轨道上,但它与太多前面的单词匹配。我尝试使用{1}仅匹配前面的一个单词,但这种语法不起作用。
matches <- phrases_with_woman %>%
filter(str_detect(phrase, "^woman|(?<=woman\\s)\\w+"))
Run Code Online (Sandbox Code Playgroud)
感谢帮助。
我正在尝试编写一个正则表达式,它只能匹配那些每个空格分隔的标记恰好出现两次的行,无论顺序如何。
例如,以下整行应该匹配:
1 1 2 2
100 10 10 100
A B B A
HELLO HELLO
Run Code Online (Sandbox Code Playgroud)
以下行不应匹配:
hello hello hello
1 1 22
1001
Run Code Online (Sandbox Code Playgroud)
尽管我能够使用 regex 匹配给定行中的各个重复组(\d+)(?=.*(\1)),但我很难使用^$. 我的猜测是,当我使用前瞻时,这会创建一个无限循环,我们不断地查看每个标记(包括重复)并期望稍后在字符串中重复,尽管我不确定如何解决这个问题。有任何想法吗?谢谢!
[编辑]:根据评论中的问题添加一些细节:显然,在大多数编程语言中将其实现为函数是相当简单的。然而,我最初希望将其实现为正则表达式,因为我试图匹配数据库中的某些记录。因此,这个正则表达式旨在作为 CASE 语句嵌入到 SQL 查询中,在我看来,这将是进行选择的好方法。
考虑到这种正则表达式的明显复杂性,似乎创建一个函数是可行的方法,因此几乎任何以下答案都将是很好的解决方案,具体取决于具体情况。