简体   繁体   English

如何在python中完全匹配两个文本行?

[英]How to match two text lines exactly in python?

I am working on a log file filter. 我正在使用日志文件过滤器。 I want to check whether several input text lines are existing in the log file.Followings are some of my text lines. 我想检查日志文件中是否存在几个输入文本行。以下是我的一些文本行。

Instrument ID = 121212121
Book Definition ID = NORMAL
Trader ID = TRQ01
Order Type = 2
Source ID = <Unset>
Instrument Index = <Unset>
Value = <Unset>
Side = 2
Qualifier = <Unset>
Order Qty = <Unset>
Visible Size = <Unset>
TIF = 0

Following is a part of my log file. 以下是我的日志文件的一部分。

~|A|SMsg:Sequenced Message{
~|A|{
~|A|    Routing Seq = 28545
~|A|    Origin = 1
~|A|    Transaction ID = 28483
~|A|    Sequenced Message = ~|A|        SMsgLite:NEW NEW ORDER
~|A|    {
~|A|            Instrument ID = 121212121
~|A|            Book Definition ID = NORMAL
~|A|            Trader ID = TRQ01
~|A|            Order Type = 2
~|A|            Source ID = <Unset>
~|A|            Instrument Index = <Unset>
~|A|            Value = <Unset>
~|A|            Side = 2
~|A|            Qualifier = <Unset>
~|A|            Order Qty = <Unset>
~|A|            Visible Size = <Unset>
~|A|            TIF = 0
~|A|            Order Sub Type = 3
~|A|            Inactive Time = <Unset>
~|A|            Expiration Date = <Unset>
~|A|            Contingent Value = <Unset>
~|A|            Owner ID = TRQ01
~|A|            Client Order ID = 380-6XAC3Vw6W
~|A|            Transact Time = <Unset>
~|A|            Symbol = <Unset>
~|A|            IsSurveillance = 1
~|A|            Reason = sd
~|A|            Gateway Rejection = <Unset>
~|A|            Order Reject Code = <Unset>

I have two main problems 我有两个主要问题

  1. I want to ignore "~|A| " character set when I am matching. 我想在匹配时忽略"~|A| "字符集。 That means I want to start matching at the 5th character position of the line. 这意味着我要在该行的第5个字符位置开始匹配。

  2. I wan to find the complete matches for above text lines. 我想找到上述文本行的完全匹配。 Here is my lookup function 这是我的查询功能

    Return the boolean True if there a match 如果存在匹配项,则返回布尔值True

     def BooleanLookup(self,infile,regex,start,end): self.infile = infile self.regex = regex self.start = start self.end = end for line in itertools.islice(infile,start,end): line = line.rstrip() if re.match(regex, line): return True break else: return False 

But it returns true even the times that part of string is matches. 但是,即使字符串的一部分匹配,它也会返回true。 How do I implement a method to get a completely match. 如何实现获得完全匹配的方法。

re.match(regex, line) will return an object if line partially contains regex from left. 如果行从左开始部分包含regex,则re.match(regex,line)将返回一个对象。 If you want to use your code, change 如果要使用代码,请更改

if re.match(regex, line):

to

if re.match(regex, line) and re.match(regex,line).span()[1] = len(line):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM