我需要匹配所有这些开始标记:
<p>
<a href="foo">
Run Code Online (Sandbox Code Playgroud)
但不是这些:
<br />
<hr class="foo" />
Run Code Online (Sandbox Code Playgroud)
我想出了这个,并希望确保我做对了.我只抓住了a-z
.
<([a-z]+) *[^/]*?>
Run Code Online (Sandbox Code Playgroud)
我相信它说:
/
,然后我有这个权利吗?更重要的是,你怎么看?
有人能以一种可以理解的方式解释这两个术语吗?
例如,这个正则表达式
(.*)<FooBar>
Run Code Online (Sandbox Code Playgroud)
将匹配:
abcde<FooBar>
Run Code Online (Sandbox Code Playgroud)
但是如何让它在多行中匹配呢?
abcde
fghij<FooBar>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用正则表达式将字符串分成两部分.字符串格式如下:
text to extract<number>
Run Code Online (Sandbox Code Playgroud)
我一直在使用(.*?)<
,<(.*?)>
哪个工作正常,但在阅读了一点regex之后,我才开始想知道为什么我需要?
表达式中的.我通过这个网站找到它们之后才这样做,所以我不确定它们之间的区别.
tl; dr:是否可以制作可重复使用的模板文字?
我一直在尝试使用模板文字,但我想我只是没有得到它,现在我感到沮丧.我的意思是,我认为我得到它,但"它"不应该是它如何工作,或它应该如何得到.应该有所不同.
我看到的所有示例(甚至是标记模板)都要求"替换"在声明时而不是运行时完成,这对我来说对于模板来说似乎完全没用.也许我很疯狂,但对我来说,一个"模板"是一个包含令牌的文档,当你使用它时,它们会被替换,而不是在你创建它时,否则它只是一个文档(即一个字符串).模板与令牌一起存储为令牌,当您评估它们时,将评估这些令牌.
每个人都引用了一个可怕的例子,类似于:
var a = 'asd';
return `Worthless ${a}!`
Run Code Online (Sandbox Code Playgroud)
这很好,但如果我已经知道a
,我会return 'Worthless asd'
或者return 'Worthless '+a
.重点是什么?认真.好吧,重点是懒惰; 更少的优点,更多的可读性.大.但那不是模板!不是恕我直言.MHO就是最重要的!问题,恕我直言,模板在声明时被评估,所以,如果你这样做,恕我直言:
var tpl = `My ${expletive} template`;
function go() { return tpl; }
go(); // SPACE-TIME ENDS!
Run Code Online (Sandbox Code Playgroud)
由于expletive
未声明,因此输出类似的内容My undefined template
.超.实际上,至少在Chrome中,我甚至无法声明模板; 它会抛出错误,因为expletive
没有定义.我需要的是能够在声明模板后进行替换:
var tpl = `My ${expletive} template`;
function go() { return tpl; }
var expletive = 'great';
go(); // My great template
Run Code Online (Sandbox Code Playgroud)
但是,我不知道这是如何可能的,因为这些不是真正的模板.即使你说我应该使用标签,nope,它们也不起作用:
> explete = function(a,b) { console.log(a); …
Run Code Online (Sandbox Code Playgroud) 我想将标签上的字符串拆分成不同的部分.
$string = 'Text <img src="hello.png" /> other text.';
Run Code Online (Sandbox Code Playgroud)
下一个功能还没有以正确的方式工作.
$array = preg_split('/<img .*>/i', $string);
Run Code Online (Sandbox Code Playgroud)
输出应该是
array(
0 => 'Text ',
1 => '<img src="hello.png" />',
3 => ' other text.'
)
Run Code Online (Sandbox Code Playgroud)
我应该用什么样的模式来完成它?
编辑 如果有多个标签怎么办?
$string = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
$array = preg_split('/(<img .*>)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
Run Code Online (Sandbox Code Playgroud)
输出应该是:
array (
0 => 'Text ',
1 => '<img src="hello.png" />',
3 => 'hello ',
4 => '<img src="bye.png" />',
5 => ' other text.' …
Run Code Online (Sandbox Code Playgroud) 我一直在使用-Wd
Python的说法,发现吨的变化我需要为了准备我升级到2.0的Django
python -Wd manage.py runserver
Run Code Online (Sandbox Code Playgroud)
主要on_delete
是由于成为必需的参数。
RemovedInDjango20Warning: on_delete 将是 Django 2.0 中 ForeignKey 的必需参数。
models.CASCADE
如果您想保持当前的默认行为,请将其设置为on models 和 in existing migrations。请参阅https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.ForeignKey.on_delete
是否有一个简单的正则表达式(或方法)可以用来放入on_delete
我的所有外键?
它应该匹配,"abababab"
因为"ab"
连续重复两次以上,但代码没有打印任何输出。在 C++ 中使用正则表达式还有其他技巧吗?
我尝试了其他语言,效果很好。
#include<bits/stdc++.h>
int main(){
std::string s ("xaxababababaxax");
std::smatch m;
std::regex e ("(.+)\1\1+");
while (std::regex_search (s,m,e)) {
for (auto x:m) std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个字符串,可以是以下两种形式之一:
name multi word description {...}
Run Code Online (Sandbox Code Playgroud)
要么
name multi word description [...]
Run Code Online (Sandbox Code Playgroud)
其中{...}
和[...]
是有效的JSON。我对仅解析字符串的JSON部分感兴趣,但是我不确定做到这一点的最佳方法(尤其是因为我不知道字符串将是两种形式中的哪种)。这是我目前的方法:
import json
string = 'bob1: The ceo of the company {"salary": 100000}'
o_ind = string.find('{')
a_ind = string.find('[')
if o_ind == -1 and a_ind == -1:
print("Could not find JSON")
exit(0)
index = min(o_ind, a_ind)
if index == -1:
index = max(o_ind, a_ind)
json = json.loads(string[index:])
print(json)
Run Code Online (Sandbox Code Playgroud)
它有效,但是我不禁觉得它可以做得更好。我以为可能是正则表达式,但是在匹配子对象和数组而不是最外层的json对象或数组时遇到了麻烦。有什么建议么?
我有点困难preg_match()
在php中捕获一个组.
这是我的模式:
<ns2:uniqueIds>(.*)<\/ns2:uniqueIds>
Run Code Online (Sandbox Code Playgroud)
这是来源:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body><ns2:ListResponse xmlns:ns2="http://censored"><ns2:uniqueIds>censored</ns2:uniqueIds><ns2:uniqueIds>censored</ns2:uniqueIds></ns2:ListResponse></env:Body></env:Envelope>
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
我正在努力将Perl cgi脚本转换为Python.
在脚本里面,我遇到了下面的代码,我不明白.
帮我解决这个问题.
my $p = $0;
$p =~ s|.*/||;
Run Code Online (Sandbox Code Playgroud)
我明白,my $p = $0;
将脚本文件的绝对路径+名称分配给变量p.
第二行将执行正则表达式替换,并仅提供没有路径的文件名.
但我不明白的是那些|.*/||
之后s
.
我搜索了很多资源但却不明白它的作用.
为了给我提供脚本名称,实际上要做的是什么?
在SQL Server中,如果我有两个表,每个表有(25)列,如何通过SELECT更新表?
查询是:
UPDATE
Table1
SET
Table1.col1 = Table2.col1,
Table1.col2 = Table2.col2,
...
...
...
Table1.col25 = Table2.col25,
FROM
Table1
INNER JOIN
Table2
ON
Table1.id = Table2.id
Run Code Online (Sandbox Code Playgroud)
但有没有办法更新所有列而不写上面的例子所有25列?
regex ×10
php ×2
regex-greedy ×2
.net ×1
c++ ×1
django ×1
ecmascript-6 ×1
explode ×1
html ×1
html-parsing ×1
javascript ×1
multiline ×1
non-greedy ×1
path ×1
perl ×1
preg-match ×1
python ×1
python-3.x ×1
regex-group ×1
replace ×1
split ×1
sql ×1
sql-server ×1
sql-update ×1
substitution ×1
xhtml ×1