在Python中使用正则表达式,如何验证用户的密码是:
注意,所有字母/数字/特殊字符都是可选的.我只想验证密码长度至少为8个字符,并且仅限于字母/数字/特殊字符.如果用户选择,则由用户选择更强/更弱的密码.到目前为止我所拥有的是:
import re
pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$"
password = raw_input("Enter string to test: ")
result = re.findall(pattern, password)
if (result):
print "Valid password"
else:
print "Password not valid"
Run Code Online (Sandbox Code Playgroud) 假设我有一个包含大量随机内容的字符串,如下所示:
strJunk ="asdf2adsf29Value=five&lakl23ljk43asdldl"
Run Code Online (Sandbox Code Playgroud)
而且我有兴趣获得位于'Value ='和'&'之间的子串,在这个例子中它将是'5'.
我可以使用如下的正则表达式:
match = re.search(r'Value=?([^&>]+)', strJunk)
>>> print match.group(0)
Value=five
>>> print match.group(1)
five
Run Code Online (Sandbox Code Playgroud)
为什么match.group(0)是整个'Value = five'而group(1)只是'five'?我有办法让'五'成为唯一的结果吗?(这个问题源于我对正则表达式的一种微弱的把握)
我也将不得不在这个字符串中进行替换,如下所示:
val1 = match.group(1)
strJunk.replace(val1, "six", 1)
Run Code Online (Sandbox Code Playgroud)
产量:
'asdf2adsf29Value=six&lakl23ljk43asdldl'
Run Code Online (Sandbox Code Playgroud)
考虑到我计划一遍又一遍地执行上述两个任务(在'Value ='和'&'之间找到字符串,以及替换该值),我想知道是否还有其他更有效的方法来寻找substring并在原始字符串中替换它.我很好地坚持我所拥有的,但我只是想确保如果有更好的方法,我不会花费更多的时间.
假设我想使用一些很棒的go包.我可以通过以下方式加入:
import "github.com/really-awesome/project/foobar"
Run Code Online (Sandbox Code Playgroud)
在该项目的foobar.go文件中,它定义了一些cgo指令,如:
#cgo windows CFLAGS: -I C:/some-path/Include
#cgo windows LDFLAGS: -L C:/some-path/Lib -lfoobar
Run Code Online (Sandbox Code Playgroud)
但如果我在foobar其他地方安装了C依赖项,我真的需要这些行说:
#cgo windows CFLAGS: -I C:/different-path/Include
#cgo windows LDFLAGS: -L C:/different-path/Lib -lfoobar
Run Code Online (Sandbox Code Playgroud)
有没有办法覆盖或特朗普在哪里cgo寻找这些依赖?现在我的修复是在运行后手动编辑这两行,go get ./...这将获取github.comreally-awesome/project/foobar代码.
注意:我正在使用MinGw编译器,但我怀疑这很重要.
我试过添加标志来构建无济于事:
go build -x -gcflags="-I C:/different/include -L C:/different-path/lib -lfoobar"
go build -x -ccflags="-I C:/different/include" -ldflags="-L C:/different-path/lib -lfoobar"
Run Code Online (Sandbox Code Playgroud)
通过-x参数,我看到了标志的打印输出,它们不包括我在命令行上设置的标志.也许#cgo CFLAGS/LDFLAGS外部go包顶部的陈述压制我告诉它使用的...
将unsigned short转换为char*(即将25转换为'25')的高效,便携方式是什么.
我想避免使用诸如获取(std :: string)字符串之类的内容.在这种情况下,性能很重要,因为这种转换需要快速而且经常发生.
我正在研究诸如使用sprintf之类的东西,但我想探索任何和所有的想法.
我在C++的枚举中遇到了一些不寻常的(至少对我而言)行为.我在Visual Studio 2008和g ++版本4.4.3中尝试了以下内容
#include <iostream>
using namespace std;
enum testEnum
{
// no zero enum
one = 1,
two = 2,
three = 3
};
int main(int argc, char *argv[])
{
testEnum e; // undefined value (may be zero, but thats just luck)
cout << "Uninitialized enum e = " << e << endl;
/*
testEnum e2(0); // error converting from int to enum
*/
testEnum e3(testEnum(0)); // forces zero !?!!?!?
cout << "zero enum e3 = " << …Run Code Online (Sandbox Code Playgroud) 我正在尝试访问由Google应用引擎中的db.ReferenceProperty链接的对象.这是模型的代码:
class InquiryQuestion(db.Model):
inquiry_ref = db.ReferenceProperty(reference_class=GiftInquiry, required=True, collection_name="inquiry_ref")
Run Code Online (Sandbox Code Playgroud)
我试图通过以下方式访问它:
linkedObject = question.inquiry_ref
Run Code Online (Sandbox Code Playgroud)
然后
linkedKey = linkedObject.key
Run Code Online (Sandbox Code Playgroud)
但它不起作用.有人可以帮忙吗?
假设我有一个unsigned char*,我们称之为:some_data
unsigned char* some_data;
Run Code Online (Sandbox Code Playgroud)
some_data中包含类似url的数据.例如:
"aasdASDASsdfasdfasdf&Foo=cow&asdfasasdfadsfdsafasd"
Run Code Online (Sandbox Code Playgroud)
我有一个函数可以获取'foo'的值,如下所示:
// looks for the value of 'foo'
bool grabFooValue(const std::string& p_string, std::string& p_foo_value)
{
size_t start = p_string.find("Foo="), end;
if(start == std::string::npos)
return false;
start += 4;
end = p_string.find_first_of("& ", start);
p_foo_value = p_string.substr(start, end - start);
return true;
}
Run Code Online (Sandbox Code Playgroud)
麻烦的是我需要一个字符串传递给这个函数,或者至少一个char*(可以转换为字符串没问题).
我可以通过强制转换解决这个问题:
reinterpret_cast<char *>(some_data)
Run Code Online (Sandbox Code Playgroud)
然后把它传递给函数okie-dokie
...
直到我使用valgrind并发现这可能导致细微的内存泄漏.
Conditional jump or move depends on uninitialised value(s) __GI_strlen
Run Code Online (Sandbox Code Playgroud)
从我收集的内容来看,它与重新解释的内容有关,搞乱了表示字符串结尾的空值.因此,当c ++试图找出字符串的长度时,搞砸了.
鉴于我无法改变some_data由unsigned char*表示的事实,有没有办法使用我的grabFooValue函数而没有这些微妙的问题?
我更喜欢保留我已经拥有的值查找函数,除非有明显更好的方法来从这个(有时很大的)unsigned char*中删除foo值.
尽管unsigned char*some_data的变化很大,有时也很大,我可以假设'foo'的值在早期的某个地方,所以我的想法是尝试获得前面X个字符的char*无符号的字符*.这可能通过让我设置char*结束的位置来消除字符串长度问题.
我尝试使用strncpy和cast的组合,但到目前为止还没有骰子.有什么想法吗?