Link to home
Start Free TrialLog in
Avatar of Greg_L_WER
Greg_L_WERFlag for Canada

asked on

Regex - Ignoring Spaces

Good morning... I'm fairly new to regular expressions and am trying to edit code that another developer built to accommodate some new text.  The way that the text is being saved in our new process is removing some of the spaces for some reason so I'm now needing to modify the Regex's to accommodate this change.  In the OLD Regex there are spaces in the statement where the spaces exist in the string... running this Regex on the new string returns nothing.  I updated the Regex to remove the spaces and it does work though I need this code to run against old and new strings.  I'm hoping that there is a way to modify the expression to work in both situations.  Any help would be appreciated.  Please let me know if you need additional clarification.

Thanks,
Greg

[b]CURRENT CODE[/b] - Note the space after Number:_   and after the number 440_

String being used "....Corporate Access Number: 201093440 \r\nLegal Entity Name...."
Regex canRegex = new Regex(@"Corporate Access Number: (?<desc>.+?) \r\nLegal Entity Name", RegexOptions.IgnoreCase, timeOut);

[b]NEW CODE[/b] - In the new version of the text the output removes the spaces in both places 

String being used "....Corporate Access Number:201100146\r\nLegal Entity Name...."
Regex canRegex = new Regex(@"Corporate Access Number:(?<desc>.+?)\r\nLegal Entity Name", RegexOptions.IgnoreCase, timeOut);

Open in new window

Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Regex canRegex = new Regex(@"Corporate Access Number: *(?<desc>.+?) *\r\nLegal Entity Name", RegexOptions.IgnoreCase, timeOut);

Open in new window

HTH,
Dan
Can you use this RegEx?
Regex canRegex = new Regex(@"Corporate Access Number: \d+( )*[\n\r]+Legal Entity Name....", RegexOptions.IgnoreCase, timeOut);

Open in new window

@Shawn: I thing the OPs code uses the "desc" group somewhere.
Yup OP might, but thought worthwhile to post because OP also said another developer built it and requirements might have changed
Avatar of Greg_L_WER

ASKER

Thanks everyone...

Dan Craciun - I tried your suggestion and it did return a value but was missing some data.  I edited it slightly by using \s* in place of any spaces and it seems to be working in both cases.  Can you foresee any issues that this could cause or would this work in all situations?

Thanks,
Greg

Regex canRegex = new Regex(@"Corporate Access Number:\s*(?<desc>.+?)\s*\r\nLegal Entity Name", RegexOptions.IgnoreCase, timeOut);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Dan... could very well be another character... in debug it just appears to be a space.  Thanks for help and confirming that it will meet my needs.