- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
作为 documentation声明,使用 regex.search(string, pos, endpos)
并不完全等同于对字符串进行切片,即 regex.search(string[pos:endpos])
。它不会匹配 的正则表达式,好像 字符串从 pos
开始,所以 ^
不匹配 子字符串的开头,但只匹配整个字符串的真正开头。但是,$
匹配子字符串的结尾或整个字符串。
>>> re.compile('^am').findall('I am falling in code', 2, 12)
[] # am is not at the beginning
>>> re.compile('^am').findall('I am falling in code'[2:12])
['am'] # am is the beginning
>>> re.compile('ing$').findall('I am falling in code', 2, 12)
['ing'] # ing is the ending
>>> re.compile('ing$').findall('I am falling in code'[2:12])
['ing'] # ing is the ending
>>> re.compile('(?<= )am').findall('I am falling in code', 2, 12)
['am'] # before am there is a space
>>> re.compile('(?<= )am').findall('I am falling in code'[2:12])
[] # before am there is no space
>>> re.compile('ing(?= )').findall('I am falling in code', 2, 12)
[] # after ing there is no space
>>> re.compile('ing(?= )').findall('I am falling in code'[2:12])
[] # after ing there is no space
>>> re.compile(r'\bm.....').findall('I am falling in code', 3, 11)
[]
>>> re.compile(r'\bm.....').findall('I am falling in code'[3:11])
['m fall']
>>> re.compile(r'.....n\b').findall('I am falling in code', 3, 11)
['fallin']
>>> re.compile(r'.....n\b').findall('I am falling in code'[3:11])
['fallin']
我的问题是...为什么 beginning 和 ending 匹配之间不一致?为什么使用pos
和endpos
把end当作真正的结束,而start/beginning没有被处理作为真正的开始/开始?
有没有什么方法可以使用 pos
和 endpos
模拟切片?因为 Python copies string when slicing在多次处理大字符串时,使用 pos
和 endpos
而不是切片会更有效,而不是仅仅引用旧的。
最佳答案
起始位置参数pos
例如,对于进行词法分析器特别有用。使用 [pos:]
对字符串进行切片的性能差异并使用 pos
参数可能看起来微不足道,但事实并非如此;例如,参见 JsLex lexer 中的此错误报告.
确实,^
匹配字符串的真正开头;或者,如果 MULTILINE
被指定,也在行首;这也是设计使然,因此基于正则表达式的扫描器可以轻松区分真正的行首/输入开头和一行上/输入内的其他点。
请注意,您也可以使用 regex.match(string[, pos[, endpos]])
函数将匹配锚定到 pos
指定位置的开始字符串 或 ;因此而不是做
>>> re.compile('^am').findall('I am falling in code', 2, 12)
[]
您通常会将扫描仪实现为
>>> match = re.compile('am').match('I am falling in code', 2, 12)
>>> match
<_sre.SRE_Match object; span=(2, 4), match='am'>
然后设置pos
至match.end()
(在这种情况下返回 4)用于连续的匹配操作。
必须从 pos
开始找到匹配项。 :
>>> re.compile('am').match('I am falling in code', 1, 12)
>>>
(请注意 .match
如何像隐式 ^
一样锚定在输入的开头,而不是在输入的末尾;实际上,这通常是错误的来源,因为人们认为匹配同时具有隐式^
和 $
- Python 3.4 添加了执行此操作的 regex.fullmatch
)
至于为什么endpos
参数与pos
不一致- 我不确切知道,但这对我来说也很有意义,因为在 Python 2 中没有 fullmatch
并在那里锚定 $
是确保整个跨度必须匹配的唯一方法。
关于python - 为什么在 Python 中的子字符串 "not completely equivalent to slicing the string"中进行正则表达式搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30999922/
试图自学,但发现很难找到示例,我的大脑已经陷入了困境。非常不确定 3 和 4,需要帮助才能使 5 正常工作。 package main import "fmt" func main () {
我有一个 slice ,它由字符串类型的 slice 组成。我希望能够为这片 slice 的各个元素赋值,不一定按顺序。然后,稍后,我希望能够更改任何特定元素的值。我已经阅读了有关 slice 的相同
我正在尝试将整数 slice append 到由整数 slice 组成的 slice 。当我打印 slice 时,它按预期显示。但是,当我将 slice append 到一片 slice 时,内容会发
我读了go slice usage and internals和 Slice和 Effective go#slice但是没有像这样用 3 个数字 slice 的方法:slice[a:b:c] 例如这段
这个问题在这里已经有了答案: Are slices passed by value? (5 个答案) 关闭 8 个月前。 如果可能的话,我正在努力使我的代码更具性能以节省内存 我做了一些研究,但找不
我是 Golang 的新手。 当我尝试它时,出现编译错误: cannot use a.B (type []*C) as type []Z in field value 代码: package main
我有数据结构: type PosList []int type InvertedIndex struct { Capacity int Len int IndexList [
我在 Go 中使用矩阵乘法进行一些性能实验并遇到了一些意想不到的结果。 版本 1: func newMatrix(n int) [][]int { m := make([][]int, n)
文档涵盖了 slice() 的 3 种用法对象: obj[:stop] = obj[slice(stop)] obj[start:stop] = obj[slice(start, stop)] obj
我有以下表示网页的结构 type Webpage struct { url.URL references []url.URL } 我想将网站表示为网页的集合。我正在使用这个结构,但感觉
我有一个这样的结构: type Headers struct { header string valueFromCalculation string
我正在参观 Golang 网站,我正在尝试消化其中一个示例。目前还不清楚它是如何工作的: package main import "fmt" func main() { s := []int{
我很好奇解包 slice 并将它们作为参数发送给可变参数函数。 假设我们有一个带有可变参数的函数: func unpack(args ...interface{}) 如果我们不想传入它工作的接口(in
我正在尝试解码来自服务器的 gzip 响应,该响应是一个 msgpack 数组或最终被 gzip 压缩的 msgpack 数组。 为了说明这一点,我的回复看起来像这样: gzip(msgpack([m
我是 Go 编程的新手。我在 Go 编程书籍中读到 slice 由三部分组成:指向数组的指针、长度和容量。 我很困惑: nil slice ( slice 没有可指向的底层数组,len = 0,cap
在Go Programming Language书中,作者给出了append()函数的以下代码示例,该函数接受[]int和int作为参数,并将相应地处理调整大小: // gopl.io/ch4/app
我在代码高尔夫游戏中尝试优化字符串复数时遇到了这个怪癖。我的想法是将字符串写成复数形式,然后使用 substr 有条件地切断最后一个字符: var counter = 1; var myText =
我有一个字符串数组:slice1 [][]string。我使用 for 循环获得了我想要的值: for _, i := range slice1 { //[string1 string2] f
我正在尝试实现一个将 TCP 端口 slice 拆分为 x 个其他 slice 的功能。这些 slice 将发送给将扫描这些端口的工作人员,因此 x 由工作人员的数量设置。 这是代码: // crea
我有以下代码 func Sum(a []int) int { res := 0 for _, n := range a { res += n } ret
我是一名优秀的程序员,十分优秀!