我正在整理一个相当复杂的正则表达式.表达式的一部分匹配字符串,例如'+ a',' - 57'等.A +或a - 后跟任意数量的字母或数字.我想匹配匹配此模式的0个或更多字符串.
这是我提出的表达方式:
([\+-][a-zA-Z0-9]+)*
Run Code Online (Sandbox Code Playgroud)
如果我使用这种模式搜索字符串'-56 + a',我希望得到两个匹配:
+ a和-56
但是,我只返回了最后一场比赛:
>>> m = re.match("([\+-][a-zA-Z0-9]+)*", '-56+a')
>>> m.groups()
('+a',)
Run Code Online (Sandbox Code Playgroud)
看看python文档,我看到:
如果一个组匹配多次,则只能访问最后一个匹配:
Run Code Online (Sandbox Code Playgroud)>>> m = re.match(r"(..)+", "a1b2c3") # Matches 3 times. >>> m.group(1) # Returns only the last match. 'c3'
所以,我的问题是:如何做您可以访问多个小组赛?
我想看看Python计算平方根的方式,所以我试图找到它的定义math.sqrt(),但我无法在任何地方找到它.我看过的_math.c,mathmodule.c和其他地方.
我知道python使用C的数学函数,但它们是在Python发行版中的某个地方,还是在其他地方链接到代码?我使用的是Mac OS X.
算法在math.sqrt()哪里?
让我们假设我们有一个代表二进制分数的字符串,例如:
".1"
Run Code Online (Sandbox Code Playgroud)
作为十进制数,这是0.5.Python中是否有一种标准方法可以将这些字符串转换为数字类型(无论是二进制还是十进制都不是非常重要).
对于整数,解决方案很简单:
int("101", 2)
>>>5
Run Code Online (Sandbox Code Playgroud)
int()接受一个可选的第二个参数来提供基数,但float()没有.
我正在寻找功能相同(我认为)的东西:
def frac_bin_str_to_float(num):
"""Assuming num to be a string representing
the fractional part of a binary number with
no integer part, return num as a float."""
result = 0
ex = 2.0
for c in num:
if c == '1':
result += 1/ex
ex *= 2
return result
Run Code Online (Sandbox Code Playgroud)
我认为这样做我想要的,虽然我可能错过了一些边缘案例.
是否有内置或标准的方法在Python中执行此操作?
我正在阅读Jon Erickson撰写的"黑客:剥削艺术"一书.
在本书的一部分中,他给出了C代码,然后使用gdb遍历相应的程序集,解释了指令和内存活动.
我在Mac OS X中工作,所以事情与他在书中提出的有点不同(他使用的是Linux).
无论如何,我有这个C程序:
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 char str_a[20];
7
8 strcpy(str_a, "Hello, world!\n");
9 printf(str_a);
10 }
Run Code Online (Sandbox Code Playgroud)
这是相应的otool对象转储(我刚刚包含main):
_main:
0000000100000ea0 pushq %rbp
0000000100000ea1 movq %rsp,%rbp
0000000100000ea4 subq $0x30,%rsp
0000000100000ea8 movq 0x00000189(%rip),%rax
0000000100000eaf movq (%rax),%rax
0000000100000eb2 movq %rax,0xf8(%rbp)
0000000100000eb6 leaq 0xd4(%rbp),%rax
0000000100000eba movq %rax,%rcx
0000000100000ebd movq $0x77202c6f6c6c6548,%rdx
0000000100000ec7 movq %rdx,(%rcx)
0000000100000eca movb $0x00,0x0e(%rcx)
0000000100000ece movw $0x0a21,0x0c(%rcx)
0000000100000ed4 movl $0x646c726f,0x08(%rcx)
0000000100000edb movq %rcx,0xe8(%rbp)
0000000100000edf xorb %cl,%cl
0000000100000ee1 movq …Run Code Online (Sandbox Code Playgroud)