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 ')'
然后你抓到那群人并搜索
. 它将找到顶点的实例。
python提取列表中文本_Python正则表达式:从文本文件中提取关键字后的元组列表...相关推荐
- python正则表达式提取数字比较好_python正则表达式从字符串中提取数字的思路详解...
python从字符串中提取数字 使用正则表达式,用法如下: ## 总结 ## ^ 匹配字符串的开始. ## $ 匹配字符串的结尾. ## \b 匹配一个单词的边界. ## \d 匹配任意数字. ## ...
- python正则取字符串日期_python 正则表达式获取字符串中所有的日期和时间
提取日期前的处理 1.处理文本数据的日期格式统一化 text = "2015年8月31日,衢州元立金属制品有限公司仓储公司(以下简称元立仓储公司)成品仓库发生一起物体打击事故,造成直接经济损 ...
- python 正则 匹配任意字符串_Python正则表达式匹配字符串中的任意纯数字
1.使用"\d+"匹配全数字 代码: import re zen = "Arizona 479, 501, 870. Carlifornia 209, 213, 650. ...
- python 读取鼠标选中文本_python怎么读取文本文件
python怎么读取文本文件? 文件的读取 步骤:打开 -- 读取 -- 关闭 1 >>> f = open('/tmp/test.txt') 2 >>> f.re ...
- 使用命令将logcat中的内容输出到文本文件中
为什么80%的码农都做不了架构师?>>> 网上搜集的方法,自己只是试了一下第一种,很好用,如果是/mylogcat.txt 直接保存在了d盘,我猜是直接保存在了sdk所在的盘的 ...
- python正则匹配ip地址_Python正则表达式匹配和提取IP地址
Linux No.1 IPv4 下面是IPv4的IP正则匹配实例: 简单的匹配给定的字符串是否是ip地址 import re if re.match(r"^(?:[0-9]{1,3}.){3 ...
- python获取月份字符串_python 正则表达式获取字符串中所有的日期和时间
提取日期前的处理 1.处理文本数据的日期格式统一化 text = "2015年8月31日,衢州元立金属制品有限公司仓储公司(以下简称元立仓储公司)成品仓库发生一起物体打击事故,造成直接经济损 ...
- python正则匹配字符串中的数字_Python正则表达式匹配字符串中的数字
1.使用"\d+"匹配全数字 代码: import re zen = "Arizona 479, 501, 870. Carlifornia 209, 213, 650. ...
- python正则匹配11个数字_Python正则表达式匹配字符串中的数字
导读 这篇文章主要介绍了Python正则表达式匹配字符串中的数字,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 1.使用"\d+"匹配全数字 ...
最新文章
- Java数据结构一 —— Java Collections API中的表
- session一致性架构设计极简教程
- Mysql字段类型选择
- 弯曲圆波导matlab_弯曲波导结构设计
- AutoLayout ScrollView在ios7下无法滑动
- OpenCV 高级API:TextDetectionModel和TextRecognitionModel
- 专业即时通讯工具的SEO人生发力
- activemq消息丢失_面试必问之消息中间件
- 你在成长为一个优秀的程序员吗
- [论文阅读] State-Relabeling Adversarial Active Learning
- js正则表达式--个人常用
- C# NFine开源框架 调用存储过程的实现代码
- vulnhub--ALFA: 1
- scrapy 出现400 Bad Request 问题
- 街篮最新服务器,街头篮球各区的服务器IP多少
- Verilog数码显示器00~99循环计数器电路
- 使用tps5430制作正负DC-DC降压电源,tps7a47和tps7a33制作正负LDO线性电源
- Python数据分析-绘图-2-Seaborn进阶绘图-6-回归图
- lvgl 8 中文内置字库配置
- php ci 国际化,CI 多国语言