python提取列表中文本_Python正则表达式:从文本文件中提取关键字后的元组列表...

这将是查找outter结构的主要正则表达式:

\bvertices\s*\((\s*(?:\([^)]+\)\s*)+)\)

在此之前,我们将删除所有评论。

\([^)]+\)

见演示

here

.

代码:

import re

test_str = """

/*--------------------------------*- C++ -*----------------------------------*\

| ========= | |

| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |

| \\ / O peration | Version: 5 |

| \\ / A nd | Web: www.OpenFOAM.org |

| \\/ M anipulation | |

\*---------------------------------------------------------------------------*/

FoamFile

{

version 2.0;

format ascii;

class dictionary;

object blockMeshDict;

}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 0.001;

vertices

(

(-20.6 0 -0.5)

(-20.6 25.4 -0.5) /* Some comment */

(0 -25.4 -0.5)

(0 0 -0.5)

(0 25.4 -0.5)

(206 -25.4 -0.5)

(206 0 -0.5)

(206 25.4 -0.5)

(290 -16.6 -0.5)

(290 0 -0.5)

(290 16.6 -0.5)

(-20.6 0 0.5)

(-20.6 25.4 0.5)

(0 -25.4 0.5)

(0 0 0.5)

(0 25.4 0.5)

(206 -25.4 0.5)

(206 0 0.5)

(206 25.4 0.5)

(290 -16.6 0.5)

(290 0 0.5)

(290 16.6 0.5)

/*(1 2 3 4)*/ // Commented tuple

//(1 2 3 4)

);

/* vertices commented

vertices

(

(-20.6 0 -0.5)

(-20.6 25.4 -0.5)

(0 -25.4 -0.5)

(0 0 -0.5)

(0 25.4 -0.5)

(206 -25.4 -0.5)

(206 0 -0.5)

(206 25.4 -0.5)

(290 -16.6 -0.5)

(290 0 -0.5)

(290 16.6 -0.5)

)

*/

negY

(

(2 4 1)

(1 3 0.3)

);

posY

(

(1 4 2)

(2 3 4)

(2 4 0.25)

);

posYR

(

(2 1 1)

(1 1 0.25)

);

blocks

(

hex (0 3 4 1 11 14 15 12)

(18 30 1)

simpleGrading (0.5 $posY 1)

hex (2 5 6 3 13 16 17 14)

(180 27 1)

edgeGrading (4 4 4 4 $negY 1 1 $negY 1 1 1 1)

hex (3 6 7 4 14 17 18 15)

(180 30 1)

edgeGrading (4 4 4 4 $posY $posYR $posYR $posY 1 1 1 1)

hex (5 8 9 6 16 19 20 17)

(25 27 1)

simpleGrading (2.5 1 1)

hex (6 9 10 7 17 20 21 18)

(25 30 1)

simpleGrading (2.5 $posYR 1)

);

edges

(

);

boundary

(

inlet

{

type patch;

faces

(

(0 1 12 11)

);

}

outlet

{

type patch;

faces

(

(8 9 20 19)

(9 10 21 20)

);

}

upperWall

{

type wall;

faces

(

(1 4 15 12)

(4 7 18 15)

(7 10 21 18)

);

}

lowerWall

{

type wall;

faces

(

(0 3 14 11)

(3 2 13 14)

(2 5 16 13)

(5 8 19 16)

);

}

frontAndBack

{

type empty;

faces

(

(0 3 4 1)

(2 5 6 3)

(3 6 7 4)

(5 8 9 6)

(6 9 10 7)

(11 14 15 12)

(13 16 17 14)

(14 17 18 15)

(16 19 20 17)

(17 20 21 18)

);

}

);

// ************************************************************************* //

"""

# Clean comments:

test_str = re.sub(r"//.*", '', test_str)

test_str = re.sub(r"/\*.*?\*/", '', test_str, 0, re.DOTALL)

# Match main group

matches = re.findall(r"\bvertices\s*\((\s*(?:\([^)]+\)\s*)+)\)", test_str, re.MULTILINE | re.DOTALL)

# Fetch tuples

matches2 = re.findall(r"\([^)]+\)", matches[0], re.MULTILINE | re.DOTALL)

print matches2

解释:

\b # word boundary

vertices # literal 'vertices'

\s* # 0 or more spaces (includes line feed/carriage return)

\( # literal '('

( # First capturing group

\s* # Som spaces

(?: # Group

\([^)]+\) # literal '(' + any non-')' character 1 or more times + literal ')'

\s* # extra spaces

)+ # repeated one or more times

)

\) # literal ')'

然后你抓到那群人并搜索

. 它将找到顶点的实例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值