- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我最近一直在写一个程序,其中一部分要求我从字符串中获取信息。我需要找到 1 个字母后跟 2 个数字(例如 S07)的位置,但我无法计算出它的正则表达式。
def get_season(filenames):
pattern = "^[a-zA-z]{1}[\d]{2}$"
found = re.search(filenames[0], pattern)
season_name = found.string
season = season_name[1:3]
print(season)
我知道这个信息在字符串中,但它一直给我“无”作为回应
(我不太确定代码部分的格式是否正确,在预览中它显示在同一行,但我程序中的缩进是正确的)
最佳答案
您将参数交换到 re.search()
。第一个参数是模式,而不是要匹配的字符串:
found = re.search(pattern, filenames[0])
你的图案也太宽了; A-z
也匹配 Z
(大写)和 a
(小写)之间的所有内容。正确的模式是:
pattern = "^[a-zA-Z]\d{2}$"
其中 {1}
是默认值,所以我省略了它。
如果您将其与文件名进行匹配,您可能不想使用开始或结束 anchor ,这会将匹配限制为仅精确字符串:
>>> re.search("^[a-zA-Z]\d{2}$", "S07").string
'S20'
>>> re.search("^[a-zA-Z]\d{2}$", "S07E01 - Meet the New Boss.avi") is None
True
>>> re.search("^[a-zA-Z]\d{2}$", "S07E01 - Meet the New Boss.avi") is None
True
>>> re.search("[a-zA-Z]\d{2}", "S07E01 - Meet the New Boss.avi").string
'S07E01 - Meet the New Boss.avi'
并且您想使用 .group()
来获取匹配的部分,而不是 string
(这是原始输入字符串):
>>> re.search("[a-zA-Z]\d{2}", "S07E01 - Meet the New Boss.avi").group()
'S07'
如果您只想要数字,则需要添加一个组,然后选择它。您创建一个带括号的捕获组:
>>> re.search("[a-zA-Z](\d{2})", "S07E01 - Meet the New Boss.avi").group(1)
'07'
这将选择第一组 (.group(1)
),这是 2 位数字部分周围的括号。
关于python - 使用正则表达式查找 1 个字母和 2 个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20003025/
这个问题在这里已经有了答案: Prolog - count repetitions in list (3 个答案) 关闭 7 年前。 所以我正在尝试创建一种方法来确定列表中 N 的数量。我已经试验了
使用 sscanf 或任何其他命令从分号后的文件读取的最佳方法是什么,例如,如果我的文件有 5: 4 5 6 7。如何将冒号后的值存储在数组中。此外,分号后面的整数数量可能会有所不同,即在上面给出的示
我正在尝试返回第 n 个数字。如果数字是 3 或 7 的倍数,则从 1 开始,则跳过该数字并获取下一个数字。但是,如果数字是 3 和 7 的倍数,则不会跳过该数字。 public int Multip
如何有效地从末尾获取一定数量的元素? 1 looks like 2 three!! 例如,如何获取最后 2 个 div 的内容? 最佳答案 $(document).ready(function(){
//Generate Food Personality for(i=0; i
我试图在给定的排序数组中找到最大的 K 个数。 例如:输入 -> [ 5, 12, 45, 32, 9, 20, 15]输出 -> K = 3, [45, 32, 20] 到目前为止我编写的代码返回最
两个数字表 a 和 b 被写入并按升序合并在一起,并删除重复项。现在的问题是在这个 super 表中找到比 O(n) 复杂度更好的 nth 数。 Limits 1 #include using nam
给定一个包含 N 个元素的数组 A,我需要找到对 (i,j) 使得 i 不等于 j 并且如果我们为所有对 (i, j) 然后它来到第k个位置。 示例:让 N=4 和数组 A=[1 2 3 4] 如果
给定一组跳过的数字,我需要找到该组中不存在的第 N 个数字。示例: 给定一组 [1, 4, 5] 一些结果: 对于 N = 1 结果 0 对于 N = 2 结果 2(因为 1 被跳过) 对于 N =
几个月前在亚马逊的招聘挑战中遇到了这个问题。 给定两个数字 a 和 b 及其倍数的升序列表,找出第 n 个倍数。 例如,如果 a = 4 , b = 6 和 n = 6 那么答案是 18因为列表是 4
所以我最近一直在研究 Python,我试图找到一种方法来在单个表达式中输出斐波那契数列的第 n 个数。这是我到目前为止编写的代码: (lambda f: f if f 1 # n == 2 -> 1
作业是编写一个 C++ 程序,它接受输入数字 n 并输出序列中的第 n 个数字: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 ... 这是我到目前为止想出的:
问题很简单(答案很可能):如何找到数组中最小的 2 个数字? for ( i = 1; i 关于c++ - 数组中最小的 2 个数,我们在Stack Overflow上找到一个类似的问题: ht
您可以调用Nokogiri::XML::Node#ancestors.size 来查看节点的嵌套深度。但是有没有办法确定嵌套最深的子节点的嵌套深度呢? 或者,您如何找到从一个节点下降的所有叶节点? 最
这个任务是找到n个数字的fibanocci。任务: 1.找出n个数的斐波那契数。 2.使用变量n,first=0,second=1,next,c。输入格式:使用 printf 语句。使用 scanf
我想添加每 10 个元素的数量。 例如, function myFunction() { for (var i = 1; i "; } } 输出: 1,2,3,4,5,6,7,8,9,
我想编写一个程序来计算斐波那契数列的第 n 个数,这是我使用 printf 和 scanf 完成的。但我希望更改我的程序,以便在命令行中输入序列号,而不是在程序提示时输入。这就是我想出的。它可以编译,
我有一个方案中的对象列表。每个对象都与一个可以在运行时计算的置信度值相关联。我想找到具有最高置信度值的前 50 个此类对象。示例:((WordPair1) (WordPair2)) 等等都是我的对象。
我正在寻找一种给定目标的算法,返回目标位为 0 的第 N 个数字。 例如,对于n={0,1,2,3}和target=1的输入,输出将是(二进制) 000,001,100,101 最佳答案 只写值N-1
我正在尝试创建一个函数来获取 vector 中的 3 个最大数字。例如:数字:1 6 2 5 3 7 4结果:5 6 7 我想我可以对它们进行 DESC 排序,在开始时获取 3 个数字,然后再对它们进
我是一名优秀的程序员,十分优秀!