我必须从不同的CAD系统解析一些STEP文件(ISO-10303-21),它们的结构总是不同的。这是可能出现的形式:
#95=STYLED_ITEM('',(#94),#92);
#12 = CARTESIAN_POINT ( 'NONE', ( 1.213489432997839200,
5.617300827691964000, -7.500000000000001800 ) ) ;
#263 = TEST ( 'Spaces must not be ignored here' ) ;
我认为正则表达式可以帮助我,所以我创建了这个(http://rubular.com/r/EtJ25Hfg77):
(\#\d+)\s*=\s*([A-Z_]+)\s*\(\s*(.*)*\s*\)\s*;
这给了我:
Match 1:
1: #95
2: STYLED_ITEM
3:
Match 2:
1: #12
2: CARTESIAN_POINT
3:
Match 3:
1: #263
2: TEST
3:
所以前两组正如预期的那样工作。但是我也需要这样的parantheses中的属性:
Match 1:
1: #95
2: STYLED_ITEM
3: ''
4: (#94)
5: #92
Match 2:
1: #12
2: CARTESIAN_POINT
3: 'NONE'
4: ( 1.213489432997839200, 5.617300827691964000, -7.500000000000001800 )
Match 3:
1: #263
2: TEST
3: 'Spaces must not be ignored here'
请帮我找到最后一组的正确表达(目前为(.*)
)。
答案 0 :(得分:3)
使用非商业用途的AGPL许可证JSDAI是用于处理STEP文件的免费开源java工具包
BSD许可证,所以始终免费和开源的是STEPcode项目,该项目生成C ++和python API以及示例STEP文件读取器/写入器,其他开源项目(如BRL-CAD,SCView和OpenVSP。
www.stepcode.org
OpenCasCade有C ++,pythonOCC有python,node-occ有javascript API用于处理从STEP转换的数据,也是免费和开源的。 OCE适用于更多平台,并且有更多错误修复
答案 1 :(得分:1)
我在自由间隔模式下格式化正则表达式,以便于阅读。我没有详细解释,但每行都有评论,我确信你能理解它。 :)
regexp = /(?x) # free-spacing mode
^ # assert head of string
(\#\d+) # captures the digits into Group 1
\s*=\s* # gets us past the equal and spaces
([A-Z_]+) # captures the name into Group 2
\s*\(\s*' # gets us inside the opening quote
([^']*?)' # captures the string in Group 3
(?: # start optional non-capturing group, let's call it A
\s*,\s* # get over the comma and spaces
(\([^)]*?\)) # capture parens to Group 4
(?:\s*,\s* # start optional non-capturing group, let's call it B
([^\s)]+) # capture last string to Group 5
)? # end optional non-capturing group B
)? # end optional non-capturing group A
\s*\)\s*; # close string
/
subject.scan(regexp) {|result|
# If the regex has capturing groups, subject is an array with the text matched by each group (but without the overall match)
# If the regex has no capturing groups, subject is a string with the overall regex match
}
答案 2 :(得分:0)
在这种情况下,我不认为正则表达式是可行的。 STEP是一种非常常见的格式,并且有解析器。既然你正在使用Java,为什么不看看这个:
http://www.steptools.com/support/stdev_docs/javalib/programming.html#SEC0-5-0
我认为这是您正在使用的格式,对吧?
除非您考虑整个架构,否则您必然会遇到正则表达式的问题。即使你设法解决所有问题,你也只是编写了一种解析器。为什么重新发明轮子?