0

Following is the string i get after processing the Doc File....

$$JHFIRMPHONE-91$$
$$SYSDATE-4$$
Dear  $$RCSALUTATION-5$$,
$$PAPTRANSMITTHANKS-34$$$$IPOWERPACKAGE-56$$$$CASHCARDLOADED-57$$$$CASHCARDTYPE-58$$$$ACTIVATE-59$$$$REFUNDBALANCEDUE-63$$$$FLEXPAY-68$$$$FLEXPAYCONT-67$$
$$REFBALDU$$$$EEXT-73$$
$$RALCASHCARD-64$$
$$ACRCASHCARD-65$$
$$CHECKMONEYCREDITCARD-61$$
$$NOPAYFULL-62$$
$$RAFMEMO-38$$$$RAFMEMOPT2-44$$
$$AMENDED-24$$
$$FORMSSCHEDULES-11$$
$$FORMS_LIST$$
$$FDHANDWRITTENATTACHMENTS-35$$
$$FDATTACHMENTS-26$$
$$FURNISHEDCOPIESKEEP-69$$
$$ESORDEATH-36$$
$$GOLDGUARANTEE-20$$
$$BASICGUARANTEE1-76$$$$BASICGUARANTEE2-77$$$$BASICGUARANTEE3-78$$
$$STONLYOPC-37$$
$$STATE-14$$
$$ST_DATA$$
$$STHANDWRITTENATTACHMENTS-29$$
$$STATTACHMENTS-27$$
$$TAXCOURSE-72$$
$$REWARDTYPE-82$$$$REWARDTYPE2-83$$$$REWARDTYPE3-84$$$$HEALTHBENEFITS-92$$$$HEALTHBENEFITS2-93$$$$HEALTHBENEFITS3-94$$$$HEALTHBENEFITS4-95$$$$HEALTHBENEFITS5-96$$
$$SINCERELY-40$$
$$CLOSING-16$$
$$JHTAXSERVICE-17$$
$$RAFSTARMEMO-45$$
$$RAFSTARMEMO2-97$$

What i want is,i need to get all the text values between $$ and $$.I don't know how to do this in an efficient way...i want some Regular Expression solution to solve this problem.I have very little knowledge about Regular Expression,so please guys help me in this...any efficient solution besides Regex will also do...thanks in advance...

3 Answers 3

4

Here are some regex basics, worth reading up on.

As far as your specific problem is concerned:

foreach(Match match in Regex.Matches(input, @"\$\$(.*?)\$\$"))
{
    string textValue = match.Groups[1].Value;
    // process the text value
}

The regex matches, two literal $, then arbitrarily many non-linebreak characters (only as much as necessary) to get to two more $. Since matches cannot overlap, there is no danger of getting extra matches from an ending $$ to a beginning $$.

A slightly more efficient and usually recommended regex would be:

@"\$\$((?!\$\$).)*\$\$"

It basically does the same thing, just the regex internals are a bit different (have a look into "lookaheads").

In fact, if you can guarantee that the text values will always just contain letters, digits, underscores and hyphens, you can use:

@"\$\$([\w-]+)\$\$"

Not also that for all three cases, some people prefer to replace \$\$ with \${2}, but I personally find that a bit unnecessary for two repetitions.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, you got me there ;)... I forgot to add lazy... Wait, it's not needed with my version.
You don't need to add laziness, because your character classes cannot consume the $s.
1

Actually no need for regex. Your text is easy to split by String.Split.

var parts = str.Split(new string[] {"$$"}, StringSplitOptions.RemoveEmptyEntries);

5 Comments

Will catch Dear and , as well. If you want to use split, keep the empty entries and then filter out every even index.
@m.buettner I don't see any comment stating OP doesn't want those values.
I assumed from the "between $$ and $$" that each text value is delimited by its own pair of $$. Well, I guess the OP would have to clarify that.
I think this is an rather unsafe approach, for every result you will also have an empty string result. In other words, you don't catch what you're looking for, you catch everything... needed or not
thanks for help..but i was getting "Dear" also which i didn't wanted...but regex solution did its trick...:)
1

I think \$\$([A-Z0-9\_\-]+?)\$\$ should cover the load.

You might want to check extra parameters that it allows multiline parsing.

Look at this link for more detailed information about regular-expressions

2 Comments

This doesn't catch the line $$ST_DATA$$ (I also just discovered that one now)
Ah, in that case it should be a bit looser.. I'll change it immediately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.