我想在文件中找到字符串的第二个实例。它位于文件中一行的开头,前一行以CRLF结尾。这是我得到的字符串和我想要的字符串在文件中出现的顺序。
> 9 | 10 | Optimal | 1 | HDD | B | 1/9 0/10 <<== the str i get w/find
> 10 | 11 | Optimal | 1 | HDD | A | 0/11 1/12 <<<=== the string I want
To narrow the scope, I've tried including \n and \r and the hat-sign and none of them work to return the string I want.
>>> pattern = re.compile(r' 10 \|')
>>> matches = pattern.finditer(searchstring)
>>> for match in matches:
print(match)
> <_sre.SRE_Match object; span=(793, 798), match=' 10 |'> <<== the str i get w/find
> <_sre.SRE_Match object; span=(845, 850), match=' 10 |'> <<<=== the string I want
> <_sre.SRE_Match object; span=(1488, 1493), match=' 10 |'>
> <_sre.SRE_Match object; span=(1713, 1718), match=' 10 |'>
>>>
## Another example of what I tried showing no matches found.
>>> pattern = re.compile(r'(\n) 10 \|')
>>> for match in matches:
print(match)##数据格式示例
> -------------------------------------------------------------------------
> 0/10 | 3 | HD | 6 Gbps | HUC10 | S07 | Optimal | RTEXKWKRR
> 1/4 | 4 | HD | 6 Gbps | HUC10 | S09 | Optimal | L0TBLPVGK
> 1/9 | 5 | HD | 6 Gbps | HUC10 | S09 | Optimal | HEDBPVTGK
> --------------------------------------------------------------------------
> 0/10 | 14 | 06/12/16 00:04:57 | CHECK CONDITION
> 1/9 | 37 | 07/22/18 09:14:33 | CHECK CONDITION
## Examples of what I've tried with the results
>>> with open((r'summary_a.out'), 'r') as csvfile:
searchstring = csvfile.read().strip()
pattern = re.compile(r"^10\s+\|")
matches = pattern.finditer(searchstring)
for match in matches:
print(match)
>>> No output.
## Other patterns tried with the same lack of output:
pattern = re.compile(r"\\n10\s+\|")
pattern = re.compile(r"\\n^10\s+\|")
pattern = re.compile("\\n^10\s+\|")
pattern = re.compile("^10\s+\|")
pattern = re.compile('^10\s+\|')
pattern = re.compile('/^10\s+\|')
** pattern = re.compile('^1\/9\s+\|')
>>> No output.
** The following pattern, ('^1/9 |'), yielded results but not what was expected.
** Output
> <_sre.SRE_Match object; span=(0, 0), match=''>
-------------- lines deleted ---------------------
> <_sre.SRE_Match object; span=(2525, 2525), match=''>
>>>
## This pattern, ('^0/10 |'), yields the same results as above:发布于 2019-12-15 06:20:26
如果您想在行的开头找到某个内容,则需要在表达式的开头添加一个^。所以像r"^10\s+\|"这样的东西会起作用的。检查一下演示。希望这能有所帮助。
发布于 2019-12-18 18:14:37
如果要隔离格式的第二次迭代,则需要使用此正则表达式。
import re
src = re.compile(r'(\n) 10 \|')
src.findall(searchstring)[1]使用findall可以在字符串中找到所有匹配的格式大小写。"1“将其设置为第二个。
虽然您有任何进一步的信息或代码,请更新。
https://stackoverflow.com/questions/59341632
复制相似问题