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
629 views
in Technique[技术] by (71.8m points)

regex - re.search Multiple lines Python

re.search with s or ' ' is not finding the multiline i'm trying to search for.

Portion of Source:

Date/Time:
2013-08-27 17:05:36 

----- BEGIN SEARCH -----

GENERAL DATA:
NAME:   AB12
SECTOR: 
999,999
CONTROLLED BY:  Player
ALLIANCE:   Aliance
ONLINE: 1 seconds ago
SIZE:   Large
HOMEWORLD:  NO
APPROVAL RATING:    100%
PRODUCTION RATE:    100%

RESOURCE DATA:
POWER:  0 / 0
BUILDINGS:  0 / 20
ORE:    80,000 / 80,000
CRYSTAL:    80,000 / 80,000
POPULATION: 40,000 / 40,000

BUILDING DATA:
N/A

UNIT DATA:
WYVERN(S):  100

----- END SEARCH -----

Looking at it in Notepad++ I see "BUILDING DATA:(LF)"

Full Code

lines = open('scan.txt','r').readlines()
for a in lines:
    if re.search(r"Ad", a):
        digits = a
        if re.search(r"2013", digits):
            date.append(digits[:19])
            count +=1
        elif re.search(r",", digits):
            clean = digits.rstrip()
            sector = clean.split(',')
            x.append(sector[0])
            y.append(sector[1])
    elif re.search(r"CONTROLLED BY:", a):
        player.append(a[15:].rstrip())
    elif re.search(r"ALLIANCE:", a):
        alliance.append(a[10:].rstrip())
    elif re.search(r"SIZE:", a):
        size.append(a[6:].rstrip())
    elif re.findall('BUILDING DATA:sN/A', a, re.M):
        def_grid = ''
        print "Didn't find it"
        defense.append(def_grid)
        defense_count +=1
    elif re.search(r"DEFENSE GRID", a):
        def_grid = a[16:].rstrip()
        print "defense found"
        defense_count +=1

But I am not having anything returned.

I need to put an empty spacer in when "DEFENSE GRID" doesn't exist after "BUILDING DATA:"

I know i'm missing something and I've tried reading up on re.search but i'm not able to find any thorough examples that explain how the multiline works.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
re.findall("BUILDING DATA:
N/A",a,re.MULTILINE)

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

...