Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 15-May-19 20:39pm
Comments
Richard MacCutchan 16-May-19 4:10am    
Regex will help you to find known strings or common patterns. Are you sure that is what you need in this case?
pavan patil 16-May-19 8:20am    
yes,if you see above properties they have common pattern means every property should have node number,load case,expected and but was fields and I want their values.
Richard MacCutchan 16-May-19 9:28am    
You need first to find the line with "Node number", then extract the two fields beginning "Expected" and "But was", each followed by a number.
Something like:
IF ISMATCH "Node number"
THEN
   MATCH "Expected: \d" // this returns the substring following Expected
   MATCH "But was: \d"  // dittoEND

So probably just as much work as using the String methods.

1 solution

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900