.NET Framework Bookmark and Share   
 index > Regular Expressions > how to capture some optional patterm
 

how to capture some optional patterm

say I have some groups of text lines that each group of lines have identical pattern except in the 3rd line there is an optional unique pattern that may or may not be present


Acct1, to be paid [12345upto16Dgit]
amout: $587.11 when: 2007-10-11 reference #: 1345
From: some named account[1234..8To14Digt] $687.99

Acct2, to be paid [12345upto16Dgit]
amout: $127.11 when: 2009-10-11 reference #: 1345
From: some other named account acct Bal2beDetermined[1234..8To14Digt]

trid with regex options ignore, explicit capture, Single line:

Acct1, to be paid [12345upto16Dgit]
amout: $587.11 when: 2007-10-11 reference #: 1345
From: some named account[1234..8To14Digt]

but that missed the one without acctBal
however if I remove the last lien of pattern , I will get them all
fs - new to w7
aha, got it

(?<AcctName>\r\n[A-Z]{2,40}([',]{0,1})( \w*[,.'-]{0,1}[A-Z]{0,40})*) \[(?<AcctNbr>\d*)\] +
.{2,4}amount: \$(?<Amt>{{money}})( {2,6})when: (?<DtPmt>{{yyyy-mm-dd}}) + reference #: (?<RefNbr>\d{4,8}) +
.{2,6}From: \w*(\s\w*)* \[\d{8,14}\]( \$(?<FromAcctBal>{{money}}) ){0,1}


instead of
(?<AcctName>\r\n[A-Z]{2,40}([',]{0,1})( \w*[,.'-]{0,1}[A-Z]{0,40})*) \[(?<AcctNbr>\d*)\] +
.{2,4}amount: \$(?<Amt>{{money}})( {2,6})when: (?<DtPmt>{{yyyy-mm-dd}}) + reference #: (?<RefNbr>\d{4,8}) +
.{2,6}From: \w*( \w*){2,10} \[\d{8,14}\]( \$(?<FromAcctBal>{{money}}) ){0,1}


Sorry about the typeo earlier on teh regex pattern. pasted wrong thing w/o being aware
fs - new to w7
aha, got it

(?<AcctName>\r\n[A-Z]{2,40}([',]{0,1})( \w*[,.'-]{0,1}[A-Z]{0,40})*) \[(?<AcctNbr>\d*)\] +
.{2,4}amount: \$(?<Amt>{{money}})( {2,6})when: (?<DtPmt>{{yyyy-mm-dd}}) + reference #: (?<RefNbr>\d{4,8}) +
.{2,6}From: \w*(\s\w*)* \[\d{8,14}\]( \$(?<FromAcctBal>{{money}}) ){0,1}


instead of
(?<AcctName>\r\n[A-Z]{2,40}([',]{0,1})( \w*[,.'-]{0,1}[A-Z]{0,40})*) \[(?<AcctNbr>\d*)\] +
.{2,4}amount: \$(?<Amt>{{money}})( {2,6})when: (?<DtPmt>{{yyyy-mm-dd}}) + reference #: (?<RefNbr>\d{4,8}) +
.{2,6}From: \w*( \w*){2,10} \[\d{8,14}\]( \$(?<FromAcctBal>{{money}}) ){0,1}


Sorry about the typeo earlier on teh regex pattern. pasted wrong thing w/o being aware
fs - new to w7

You can use google to search for other answers

Custom Search

More Threads

• How to pass regex backreference to function?
• Password strength: Exactly 8 characters but must contains at least 2 digits
• Removing noise words with regex problem
• Date Retrieval from string some times falls
• match LogFail(), LogPass(), Log(), but not Logger l = new Logger()
• Select lines that contain only a know set of characters
• Validate a Decimal enclosed in Parenthesis
• Help with RegEx Seek-n-Find
• Match word with (!)
• Pattern char start end