如果我有一个未知的结构字符串:
"stuff I don't care about THING different stuff I don't care about THING ... THING even more stuff I don't care about THING stuff I care about"
我想捕捉"我关心的东西",它总是在最后一次出现之后.有可能出现0次或多次.如果有0次出现则没有我关心的东西.字符串不能以THING开头或结尾.
一些可能的字符串
"stuff I don't care about THING stuff I care about"
"stuff I don't care about"
一些不可能的字符串:
"THING stuff I care about"
"stuff I don't care about THING stuff I don't care about THING"
我目前解决这个问题的方法是使用带有两个贪心量词的正则表达式,如下所示:
if( /.*THING(.*)/ ) {
$myStuff = $1;
}
Run Code Online (Sandbox Code Playgroud)
它似乎有效,但我的问题是两个贪婪量词如何相互作用.第一个(最左边)贪婪量词总是"比第二个更贪婪"吗?
基本上我保证不会得到如下分割:
"stuff I don't care about THING" …
我知道让方法返回 async void 是不好的做法,因为它很难测试,但是单元测试是否有任何理由需要返回 async Task 而不是 async void?
基本上是这样的:
[Test()]
public async void MyTest()
{
//Some code to run test
//Assert Something
}
Run Code Online (Sandbox Code Playgroud)
或者我应该这样做:
[Test()]
public async Task MyTest()
{
//Some code to run test
//Assert Something
}
Run Code Online (Sandbox Code Playgroud) 是否可以在 Python 中执行类似以下 Perl 代码的操作?据我所知,答案是否定的,但我想我会仔细检查一下。
我想在 Python 中复制的 Perl 代码:
#!/usr/bin/perl
my $line = "hello1234world";
if($line=~/hello(.*)world/) {
print($1);
}
#prints 1234
Run Code Online (Sandbox Code Playgroud)
以下是我能想到的最接近的风格,但在运行时我(显然)收到以下错误:
import re
line = "hello1234world"
if matchObj = re.match(r'hello(.*)world',line):
print(matchObj.group(1))
#error: if matchObj = re.match(r'hello(.*)world',line):
#error: ^
#error: SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
以下是我能想到的最好的工作代码:
import re
line = "hello1234world"
matchObj = re.match(r'hello(.*)world',line)
if matchObj:
print(matchObj.group(1))
#prints 1234
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我真的很想避免为变量声明和 if 语句单独一行。
我正在使用以下 vim 命令: :v/^\w*sub/s/.*\n//g
我希望它去掉任何不以开头的行,sub这样我就可以快速浏览我的 perl 脚本中的所有子标题。
它最终可以工作,但我必须“运行”该命令 8 次才能让它只留下正确的行。为什么它不只是第一次工作?不确定它是否重要,但文件是 ~1600 行,它从第一次删除 ~700 行开始,但最后一次只删除最后 2 行。
regex ×3
perl ×2
asynchronous ×1
c# ×1
greedy ×1
python ×1
quantifiers ×1
regex-greedy ×1
unit-testing ×1
vim ×1