我需要替换一个字符串,它跟在一个特定的字符串和一些不同的数据之后.我需要保持开头和中间,只需更换结束.当我尝试下面的代码时,它只替换最后一次出现.我试过切换到一个非贪婪的比赛,但后来却找不到它.中间可以包含新的行,空格,字母和数字.
String s = "Beginning of story. Keep this sentence. Old ending.\n";
s += s;
s += s;
s1 = Regex.Replace(s, @"Beginning of story. ([\s\S]*) Old ending.", "Beginning of story. " + @"$1" + " New ending.", RegexOptions.Multiline | RegexOptions.IgnoreCase);
The result is this:
Beginning of story. Keep this sentence. Old ending.
Beginning of story. Keep this sentence. Old ending.
Beginning of story. Keep this sentence. Old ending.
Beginning of story. Keep this sentence. New ending.
Run Code Online (Sandbox Code Playgroud)
如何取代每次出现的"旧结局".
我preg_match()用来从变量中提取文本片段,让我们说变量看起来像这样:
[htmlcode]This is supposed to be displayed[/htmlcode]
middle text
[htmlcode]This is also supposed to be displayed[/htmlcode]
Run Code Online (Sandbox Code Playgroud)
我想提取[htmlcode]'s 的内容并将它们输入到数组中.我这样做是通过使用preg_match().
preg_match('/\[htmlcode\]([^\"]*)\[\/htmlcode\]/ms', $text, $matches);
foreach($matches as $value){
return $value . "<br />";
}
Run Code Online (Sandbox Code Playgroud)
上面的代码输出
[htmlcode]This is supposed to be displayed[/htmlcode]middle text[htmlcode]This is also supposed to be displayed[/htmlcode]
Run Code Online (Sandbox Code Playgroud)
代替
如果真的没有想法
我想删除标签之间的内容<script></script>。我正在手动检查模式并iterating使用 while 循环。但是,我正在了解StringOutOfBoundException这一行:
String script = source.substring(startIndex,endIndex-startIndex);
Run Code Online (Sandbox Code Playgroud)
下面是完整的方法:
String script = source.substring(startIndex,endIndex-startIndex);
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么吗?我正在得到endIndex=-1。任何人都可以帮助我确定为什么我的代码被破坏。
给定以下简单的正则表达式,其目标是在引号字符之间捕获文本:
regexp = '"?(.+)"?'
Run Code Online (Sandbox Code Playgroud)
当输入是这样的:
"text"
Run Code Online (Sandbox Code Playgroud)
捕获组(1)具有以下内容:
text"
Run Code Online (Sandbox Code Playgroud)
我预计组(1)text只有(没有引号).有人可以解释发生了什么以及为什么正则表达式捕获"符号,即使它在捕获组#1之外.我不理解的另一个奇怪的行为是为什么第二个引用字符被捕获但不是第一个引用字符,因为它们都是可选的.最后我使用以下正则表达式修复它,但我想了解我做错了什么:
regexp = '"?([^"]+)"?'
Run Code Online (Sandbox Code Playgroud) 我需要拆分像这样的字符串
<p>foo</p><p>bar</p>
Run Code Online (Sandbox Code Playgroud)
到"foo"和"bar"的数组
我认为RegEx可以帮助我,但似乎我不理解RegEx.这是我的尝试.
var inputText = "<p>foo</p><p>bar</p>";
splittedSelection = inputText.split("/<p>|<\/p>/g");
Run Code Online (Sandbox Code Playgroud)
但我所能实现的只是一个带有一个条目的数组,它与inputText相同.
谢谢你的帮助.
我在 Perl 上咬牙切齿。我对正则表达式很满意(但仍然容易出错)。
为什么(*)在名为 param 的 Express 路由中用作正则表达式?
为什么(.*)在名为 param 的 Express 路由中不能用作正则表达式?
是像([\\w:./]+)一个更可靠的方式做到这一点?
我正在尝试使用旨在在值中包含斜杠的路由参数。
例如
如果请求是:
http://www.example.com/new/https://www.youtube.com/trending
Run Code Online (Sandbox Code Playgroud)
...我正在使用这条路线:
app.get('/new/:url', (req, res) => {
console.log('new')
console.log(req.params.url)
})
Run Code Online (Sandbox Code Playgroud)
我要url平等https://www.youtube.com/trending
我知道路径在斜杠上分开,所以我想我可以在命名参数后的括号中使用正则表达式来匹配斜杠。
我试过/new/:url(.*),我认为它应该贪婪地匹配任何东西,包括斜线,但这使路线完全失败。为什么这不起作用?
通过我自己的反复试验,我发现这是/new/:url([\\w:./]+)有效的。这对我来说很有意义,但似乎不必要地复杂。这是“正确的方法”吗?
最让我困惑的是我在 YouTube 视频示例中找到的一个......为什么/new/:url(*)有效?该*说0或多个之前的项目,但没有什么星号之前。
我有一种感觉,答案就在这个 GitHub 问题中,但通过阅读线程我不清楚到底发生了什么。是否(*)依赖可能在下一版 Express 中更正的错误?
我+?在正则表达式中看到了很多,但我不确定它的真正含义.我知道+手段1或更多,?手段0或1.所以呢+?手段0或更多?在那种情况下,为什么不只是使用*,这意味着0什么?
我只需要知道+?手段0或更多,或者它意味着不同的东西.然后我会删除这个问题,如果它太烦人了.
Python中是否有任何直接的方法来剥离字符串并获取起始索引和结束索引?
示例:给定字符串' hello world! ',我想要剥离的字符串'hello world!'以及起始索引2和和索引14.
' hello world! '.strip() 只返回剥离的字符串.
我可以写一个函数:
def strip(str):
'''
Take a string as input.
Return the stripped string as well as the start index and end index.
Example: ' hello world! ' --> ('hello world!', 2, 14)
The function isn't computationally efficient as it does more than one pass on the string.
'''
str_stripped = str.strip()
index_start = str.find(str_stripped)
index_end = index_start + len(str_stripped)
return str_stripped, …Run Code Online (Sandbox Code Playgroud) 我试图在句子中匹配一个可选的(可能存在的)短语:
perl -e '$_="word1 word2 word3"; print "1:$1 2:$2 3:$3\n" if m/(word1).*(word2)?.*(word3)/'
Run Code Online (Sandbox Code Playgroud)
输出:
1:word1 2: 3:word3
Run Code Online (Sandbox Code Playgroud)
我知道第一个 '.*' 是贪婪的并将所有内容匹配到 'word3'。使它不贪婪无济于事:
perl -e '$_="word1 word2 word3"; print "1:$1 2:$2 3:$3\n" if m/(word1).*?(word2)?.*(word3)/'
Run Code Online (Sandbox Code Playgroud)
输出:
1:word1 2: 3:word3
Run Code Online (Sandbox Code Playgroud)
这里似乎存在利益冲突。我原以为 Perl 会匹配 (word2)?如果可能,仍然满足非贪婪的 .*?。至少这是我对“?”的理解。Perl 正则表达式页面显示“?” 制作 1 次或零次,所以它不应该更喜欢一场比赛而不是零次吗?
更令人困惑的是,如果我捕获 .*?:
perl -e '$_="word1 word2 word3"; print "1:$1 2:$2 3:$3 4:$4\n" if m/(word1)(.*?)(word2)?.*(word3)/'
Run Code Online (Sandbox Code Playgroud)
输出:
1:word1 2: 3: 4:word3
Run Code Online (Sandbox Code Playgroud)
这里的所有组都是捕获组,所以我不知道为什么它们是空的。
只是为了确保没有捕获字间空间:
perl -e '$_="word1_word2_word3"; print "1:$1 2:$2 3:$3 4:$4\n" if m/(word1)(.*?)(word2)?.*(word3)/'
Run Code Online (Sandbox Code Playgroud)
输出:
1:word1 2: 3: 4:word3 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使子字符串可选。这是来源:
Movie TOTO S09 E22 2022 Copyright
Run Code Online (Sandbox Code Playgroud)
我想选择性地捕获子字符串:S09 E22
到目前为止我已经尝试过:
/(Movie)(.*)(S\d\d\s*E\d\d)?/gmi
Run Code Online (Sandbox Code Playgroud)
问题是它最终通过匹配S09 E22 2022 Copyright而不仅仅是S09 E22:
Match 1 : 0-33 Movie TOTO S09 E22 2022 Copyright
Group 1 : 0-5 Movie
Group 2: 5-33 TOTO S09 E22 2022 Copyright
Run Code Online (Sandbox Code Playgroud)
有办法解决这个问题吗?
问候
regex ×7
php ×2
python ×2
.net ×1
c# ×1
express ×1
html ×1
html-parsing ×1
java ×1
javascript ×1
node.js ×1
perl ×1
preg-replace ×1
split ×1
strip ×1