如何使用正则表达式查找/替换来换行,以便每行不会超过 20 个符号?
我发现了这个:
Find: \s(?<=.{20})
Replace: $0\r\n
这将是完美的,但是如果单词在 20 个符号之前开始,那么它会将大于 20 个符号的单词留在行中。
我需要一个类似的表达式,但是如果最后一项使行变大,它也应该转到新行,因此最后一行总是会有<20个符号。
我知道我很久以前就做过这个,也许是用了一些插件,但现在我无法让它工作。我该怎么做?
如何使用正则表达式查找/替换来换行,以便每行不会超过 20 个符号?
我发现了这个:
Find: \s(?<=.{20})
Replace: $0\r\n
这将是完美的,但是如果单词在 20 个符号之前开始,那么它会将大于 20 个符号的单词留在行中。
我需要一个类似的表达式,但是如果最后一项使行变大,它也应该转到新行,因此最后一行总是会有<20个符号。
我知道我很久以前就做过这个,也许是用了一些插件,但现在我无法让它工作。我该怎么做?
我正在使用 IMPORTXML 来获取一些数据,然后使用正则表达式来提取一些文本。我想要提取的部分是姓名。
它总是跟在“全名:”(减去引号)后面。名字后面总是有一个单词,后面跟着另一个冒号。这个单词可以是发音、语言学、类型,或者其他任何词。
我能得到的最接近的公式是这个:
=IFERROR(REGEXEXTRACT(REGEXREPLACE(JOIN(" ", IMPORTXML(B13, "//div[@id='meta']")), "\s+", " "), "Full Name:\s*([A-Za-z]+(?:[-'\s][A-Za-z]+)*)"), "")
不幸的是,最后一句话还是留下来了。
例如如果你有:
全名:亚伯拉罕·林肯 昵称:诚实的亚伯。
我只想让它拉出亚伯拉罕·林肯。我有这个公式输出:
亚伯拉罕·林肯 昵称
其他示例:
各自的输出:
实例: https://www.baseball-reference.com/players/l/lopezal01.shtml
公式应输出:Alfonso Ramon Lopez
https://www.baseball-reference.com/players/r/ruthba01.shtml
公式应输出:George Herman Ruth
我还需要做什么才能确保 O'Brien 和 Smith-Rogers 这样的名字不受影响?
我有 200 个包含文本行的文件,它们可能看起来像以下任何一种模式:
{hello} "this is an example" {{ config(alias="customertable") }}
{{hello}} "this is an example" {{ config ( alias = 'monthly revenue') }}
{ { config ( alias = 'record 3 breaking news') } } {{hello}} "this is an example"
{{hello}} 'this is an example' { { config( alias = "designer bags 4 u") }} {{hello}} "this is an example"
单词alias后面的字符串始终用单引号或双引号引起来。我想将_link放在该字符串的末尾,因此它看起来像这样:“customertable_link”或“monthly revenue_link”,并想使用 powershell。我希望返回整行,因此第四个示例应该返回以下内容:
{{hello}}'这是一个例子'{ { config( alias = "designer bags 4 u_link") }} {{hello}}“这是一个例子”
例如,在一行上搜索字符串别名,然后在"或'的第二个实例左侧添加_link。
这是我目前所拥有的:
$string -replace 'alias=(?:[^"]*"){2}', '$0_link'
对于第一个例子来说,这几乎是可行的(只是引用的位置不对),但我无法正确理解。任何帮助都将不胜感激!
我需要正则表达式方面的帮助。我想将所有出现的5%
或7%
或183%
或替换99%
为``(空字符串),但如果出现的是0%
或100%
(可能有几十个我想保留的出现),我不想做任何事情。
例如:
aaa 0% bbb
应该变成aaa 0% bbb
(没有改变)
然而
aaa 40% bbb
应该成为aaa bbb
我想出了一个使用负向后视的正则表达式,但它只会删除%
符号,而不会删除数字。这是正则表达式:
替换(?<!(0|100))%
为 ``(空字符串)
上述正则表达式应用于字符串时aaa 40% bbb
将返回aaa 40 bbb
。
我试图用以下公式字符串替换[
& :]
col_formula:regexp_replace( regexp_replace([`cellid`], "(.*)_N", "N"), "_(.*)", "")
var replaced_col_formula= col_formula.replaceAll("/[\\[\\]']+/g", "")
println(s"replaced_col_formula:$replaced_col_formula")
replaced_col_formula:regexp_replace( regexp_replace([`cellid`], "(.*)_N", "N"), "_(.*)", "")
我期待的是下面这样的
replaced_col_formula:regexp_replace( regexp_replace(cellid, "(.*)_N", "N"), "_(.*)", "")
我正在尝试捕获带连字符的单词中所有连字符的位置,以便我可以加载包含这些连字符位置的哈希值(在文本中,而不是单词中)。目前,我正在尝试在非捕获组内使用捕获组……但它只捕获最后一个连字符。
my $word = shift (@_);
my $word_start_pos = shift (@_);
my $text = shift (@_);
my $dash_pos = 0;
my $exp = 0;
my $pos = 0;
my $test_char = '';
if ($word =~ /^(?:[\p{L&}0-9\.\'\/]{1,}([\-])){7,}[\p{L&}0-9\.\'\/]{1,}$/) {
foreach $exp (1..$#-) {
$pos = $-[$exp];
$dash_pos = $word_start_pos + $pos;
$test_char = substr($text, $dash_pos, 1);
if ($test_char =~ /^[\-]$/) {
&load_changes('-', $dash_pos, 'Dash', ' ', 'Replace');
}
}
}
我有以下测试用例:
CPP_CSHARP_REGEX_TEST_CASES = [
("C++", True),
("C#", True),
("C+", False),
("C##", False),
("C", True),
]
我可以使用哪个正则表达式来通过 pytest?我尝试过r"^C[+{2},#{1}]$"
Tim Biegeleisen 的解决方案,但失败了C
:
________________________________________________________________________________ test_cpp_csharpRegex[C-True] ________________________________________________________________________________
data = 'C', expected = True
@pytest.mark.parametrize("data, expected", CPP_CSHARP_REGEX_TEST_CASES)
def test_cpp_csharpRegex(data, expected):
cpp_csharp_regex = r"\bC(?:\+\+|#)(?=\s|$)"
> assert expected == bool(re.match(cpp_csharp_regex, data))
E AssertionError: assert True == False
E + where False = bool(None)
E + where None = <function match at 0x7ab4e2cded40>('\\bC(?:\\+\\+|#)(?=\\s|$)', 'C')
E + where <function match at 0x7ab4e2cded40> = re.match
regex_test.py:131: AssertionError
================================================================================== short test summary info ===================================================================================
FAILED regex_test.py::test_cpp_csharpRegex[C-True] - AssertionError: assert True == False
================================================================================ 1 failed, 70 passed in 0.08s ================================================================================
Raku 有一个有趣且令人兴奋的递归正则表达式符号:<~~>
。
因此在 REPL 中我们可以这样做:
[0] > 'hellohelloworldworld' ~~ m/ helloworld /;
「helloworld」
[1] > 'hellohelloworldworld' ~~ m/ hello <~~>? world /;
「hellohelloworldworld」
直接从 Raku Docs for Recursive Regexes中获取,我们可以捕获/计算各种嵌套级别:
~$ raku -pe '#acts like cat here' nest_test.txt
not nested
previous blank
nestA{1}
nestB{nestA{1}2}
nestC{nestB{nestA{1}2}3}
~$ raku -ne 'my $cnt = 0; say m:g/ \{ [ <( <-[{}]>* )> | <( <-[{}]>* <~~>*? <-[{}]>* )> ] \} {++$cnt} /, "\t $cnt -levels nested";' nest_test.txt
() 0 -levels nested
() 0 -levels nested
() 0 -levels nested
(「1」) 1 -levels nested
(「nestA{1}2」) 2 -levels nested
(「nestB{nestA{1}2}3」) 3 -levels nested
(上面,改为say
仅put
返回捕获的字符串)。
但是我最近在尝试解决Unix 和 Linux 问题时遇到了一个问题,即:如何限制递归?假设我们只想捕获以下内容nestB
。有没有办法使用<~~>
递归正则表达式语法来做到这一点?
~$ raku -ne 'my $cnt = 0; say m:g/ nestB \{ [ <( <-[{}]>* )> | <( <-[{}]>* <~~>*? <-[{}]>* )> ] \} {++$cnt} /, "\t $cnt -levels nested";' nest_test.txt
() 0 -levels nested
() 0 -levels nested
() 0 -levels nested
() 0 -levels nested
() 0 -levels nested
() 0 -levels nested
注意:上文我尝试使用 强制执行某种“节俭的递归行为” <~~>*?
。事实是<~~>
(标准递归符号),<~~>?
、<~~>*
和<~~>*?
都给出相同的结果(rakudo-moar-2024.09-01
)。
测试字符串:
[valign px=-2][center][bgcolor=GREEN]TESTING[/bgcolor][/center][/valign]
我想出了color.*?\]
几乎可行的方法......它获取关键字之后直至结束括号的所有内容:
color=GREEN] AND color] ---> Target ---> [bgcolor=GREEN] AND [/bgcolor]
我只是不明白如何反向做同样的事情
\[.*?color.*?\]
这让我得到了键之前直到结束括号的整个字符串 -[valign px=-2][center][bgcolor=GREEN]
但它也正确地给了我它们之间[/bgcolor]
没有的TESTING
我正在使用https://regex101.com/进行测试
我想将某些短语中的空格转换为下划线,以便使用正则表达式进行查找和替换。例如,我想要这样:
If you are going to the fair a trader there will offer you a fair price
进入
如果你要去公平交易市场,那里的商人会给你一个公平的价格
我知道如何以各种方式捕获这些特定的字符串
例如
(the fair)|(a trader)|(offer you)
(the)(\s)(fair)|(a)(\s)(trader)|(offer)(\s)(you)
ETC。
但我不知道如何处理捕获组的编号,以便对其中任何一个进行相同的替换(空格到下划线)。
我尝试过:
1.
(the)(?:\s)(fair)|(a)(?:\s)(trader)|(offer)(?:\s)(you)
$1_$2
If you are going to the_fair _ there will _ a fair price
(the|a|offer)(?:\s)(fair|trader|you)
$1_$2
If you are going to the_fair a_trader there will offer_you a_fair price
我不确定还能尝试什么。如果我没有正确表达我的问题,请原谅;我对此很陌生