Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

Extract substrings separately from a string using python regex

I am trying to write a regular expression which returns a part of substring which is after a string. For example: I want to get part of substring along with spaces which resides after "15/08/2017".

a='''S
LINC             SHORT LEGAL                                   TITLE NUMBER
0037 471 661     1720278;16;21                                 172 211 342

LEGAL DESCRIPTION
PLAN 1720278  
BLOCK 16  
LOT 21  
EXCEPTING THEREOUT ALL MINES AND MINERALS  

ESTATE: FEE SIMPLE  
ATS REFERENCE: 4;24;54;2;SW

MUNICIPALITY: CITY OF EDMONTON

REFERENCE NUMBER: 172 023 641 +71

---------------------------------------------------------------------------- 
----
             REGISTERED OWNER(S)
REGISTRATION    DATE(DMY)  DOCUMENT TYPE      VALUE           CONSIDERATION
--------------------------------------------------------------------------- 
-- 
---

172 211 342    15/08/2017  AFFIDAVIT OF                       CASH & MTGE'''

Is there a way to get 'AFFIDAVIT OF' and 'CASH & MTGE' as separate strings?

Here is the expression I have pieced together so far:

doc = (a.split('15/08/2017', 1)[1]).strip()
'AFFIDAVIT OF                       CASH & MTGE'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not a regex based solution. But does the trick.

a='''S
LINC             SHORT LEGAL                                   TITLE NUMBER
0037 471 661     1720278;16;21                                 172 211 342

LEGAL DESCRIPTION
PLAN 1720278  
BLOCK 16  
LOT 21  
EXCEPTING THEREOUT ALL MINES AND MINERALS  

ESTATE: FEE SIMPLE  
ATS REFERENCE: 4;24;54;2;SW

MUNICIPALITY: CITY OF EDMONTON

REFERENCE NUMBER: 172 023 641 +71

---------------------------------------------------------------------------- 
----
            REGISTERED OWNER(S)
REGISTRATION    DATE(DMY)  DOCUMENT TYPE      VALUE           CONSIDERATION
--------------------------------------------------------------------------- 
-- 
---

172 211 342    15/08/2017  AFFIDAVIT OF                       CASH & MTGE'''

doc = (a.split('15/08/2017', 1)[1]).strip() 
# used split with two white spaces instead of one to get the desired result
print(doc.split("  ")[0].strip()) # outputs AFFIDAVIT OF
print(doc.split("  ")[-1].strip()) # outputs CASH & MTGE

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...