正则表达式用于检索字符串值 [英] Regex for retriving string values

查看:76
本文介绍了正则表达式用于检索字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Regex的新手。我需要正则表达式跟随字符串



1)属性NodeDisplacements.Dx超出可接受的容差(节点号49,载荷情况1)。

预计:0.2524431049823761d +/- 0.01d或0.2524431049823761d +/- 2.0d百分比

但是:0.24117276072502136d



2 )属性MemberEndForces.Fx超出可接受的公差范围(梁编号1,载荷工况1,梁端EndB)。

预期:-1.166001558303833d +/- 0.001d或-1.166001558303833d +/- 2.0 d百分比

但是:-1.1420921087265015d



3)物业会员部队.Mz超出可接受的公差范围(梁号33,载荷工况) 1)。

预期:< 60.961132049560547d,50.785896301269531d,40.610660552978516d,30.4354248046875d,20.260189056396484d,10.084957122802734d,-0.090282440185546875d,-10.265514373779297d,-20.440753936767578d,-30.615993499755859d,-40.791217803955078d,-50.966464996337891d,-61.141693115234375d> +/- 0.01d或< 60.961132049560547d,50.785896301269531d,40.610660552978516d,30.4354248046875d,20.260189056396484d,10.084957122802734d,-0.090282440185546875d,-10.265514373779297d,-20.440753936767578d,-30.615993499755859d,-40.791217803955078d,-50.966464996337891d,-61.141693115234375d> +/- 3.0d百分比

但是:< 61.908939361572266d,51.576515197753906d,41.244094848632812d,30.911674499511719d,20.579250335693359d,10.246833801269531d,-0.085590362548828125d,-10.418010711669922d,-20.750438690185547d,-31.082859039306641d,-41.415271759033203d,-51.747707366943359d,-62.080116271972656d>



字符串变量包含以上任何值。所以我读过节点号,载荷情况,预期但是在第一种情况下是值。在第二种情况下梁编号(节点编号不在那里)和案例1中提到的所有内容,在第三种情况下必须读取所有预期的,但是是值。



预期结果:



1)节点编号49载荷工况1预期:0.2524431049823761公差:0.01公差百分比:2.0但是:0.24117276072502136



2)梁编号1载荷工况1预期:-1.166001558303833公差:0.001公差百分比:2.0但是:-1.1420921087265015



3)梁号33装载箱1预期:< 60.961132049560547d,50.785896301269531d,40.610660552978516d,30.4354248046875d,20.260189056396484d,10.084957122802734d,-0.090282440185546875d,-10.265514373779297d,-20.440753936767578d,-30.615993499755859d,-40.791217803955078d,-50.966464996337891d,-61.141693115234375d>容差:0.01容差百分比:3.0



所以我需要一个正则表达式来处理所有上述3个案例。

请帮助。



我尝试过:



目前我能够提取这些值在Substring()的帮助下。但是需要像Regex这样的其他方法。

I'm new to Regex. And I need Regex for following string

1) Property NodeDisplacements.Dx falls outside the acceptable tolerance (Node number 49, load case 1).
Expected: 0.2524431049823761d +/- 0.01d or 0.2524431049823761d +/- 2.0d Percent
But was: 0.24117276072502136d

2) Property MemberEndForces.Fx falls outside the acceptable tolerance (Beam number 1, load case 1, beam end EndB).
Expected: -1.166001558303833d +/- 0.001d or -1.166001558303833d +/- 2.0d Percent
But was: -1.1420921087265015d

3)Property MemberSectionForces.Mz falls outside the acceptable tolerance (Beam number 33, load case 1).
Expected: < 60.961132049560547d, 50.785896301269531d, 40.610660552978516d, 30.4354248046875d, 20.260189056396484d, 10.084957122802734d, -0.090282440185546875d, -10.265514373779297d, -20.440753936767578d, -30.615993499755859d, -40.791217803955078d, -50.966464996337891d, -61.141693115234375d > +/- 0.01d or < 60.961132049560547d, 50.785896301269531d, 40.610660552978516d, 30.4354248046875d, 20.260189056396484d, 10.084957122802734d, -0.090282440185546875d, -10.265514373779297d, -20.440753936767578d, -30.615993499755859d, -40.791217803955078d, -50.966464996337891d, -61.141693115234375d > +/- 3.0d Percent
But was: < 61.908939361572266d, 51.576515197753906d, 41.244094848632812d, 30.911674499511719d, 20.579250335693359d, 10.246833801269531d, -0.085590362548828125d, -10.418010711669922d, -20.750438690185547d, -31.082859039306641d, -41.415271759033203d, -51.747707366943359d, -62.080116271972656d >

String variable contain any of above value.So I have read node number,load case,expected and but was value in 1st case. and in 2nd case beam number (as node number is not there) and everything mentioned in case 1. and in 3rd case have to read all expected and but was value.

Expected result:

1) Node number 49 load case 1 Expected: 0.2524431049823761 Tolerance:0.01 Tolerance percent :2.0 But was: 0.24117276072502136

2) Beam number 1 load case 1 Expected: -1.166001558303833 Tolerance:0.001 Tolerance percent :2.0 But was: -1.1420921087265015

3) Beam number 33 load case 1 Expected: < 60.961132049560547d, 50.785896301269531d, 40.610660552978516d, 30.4354248046875d, 20.260189056396484d, 10.084957122802734d, -0.090282440185546875d, -10.265514373779297d, -20.440753936767578d, -30.615993499755859d, -40.791217803955078d, -50.966464996337891d, -61.141693115234375d > Tolerance:0.01 Tolerance percent : 3.0

so I need a regex which will handle all above 3 cases.
Please help.

What I have tried:

currently I'm able to extract these values with the help of Substring().but need any other approach like Regex.

推荐答案

有一个老笑话就像:

我有一个问题,我想用Regex解决它。所以现在我有两个问题!



为什么你需要另一种方法而不是子串?正则表达式当然是一个强大的工具,但除非你理解它,否则你不想使用它。如果您只是复制粘贴在互联网上找到的一些片段而不了解它们,您就无法维护它并且它不会给您带来任何好处。





所以要么坚持你所拥有的,要么投入时间来学习如何使用正则表达式。



也许尝试这样的东西来学习(通过Google搜索正则表达式教程找到它。)

RegexOne - 学习正则表达式 - 第1课:简介和ABCs [ ^ ]
There is an old joke going something like:
I have a problem and I want to solve it with Regex. So now I have two problems!

Why do you need another approach than substring? Regular expressions are certainly a powerful tool, but you do not want to use it unless you understand it. If you just copy paste some snippets you find on the internet without understanding them, you can't maintain it and it won't really give you any benefits.


So either stick to what you have or invest in the time to learn how to use regex.

Maybe try something like this to learn (just found it by googling "regex tutorial")
RegexOne - Learn Regular Expressions - Lesson 1: An Introduction, and the ABCs[^]


这篇关于正则表达式用于检索字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆