none
Regular Expression Help RRS feed

  • Question

  • I've got a string that is

    "xyz,00\r\rSometext\r\n$TextIneed 5.5$\0"   or it could also be

    "xyz,00\r\$TextIneed 5.5$\0rSometext\r\n"

    I want the string between the two $ signs  "TextIneed 5.5"

    Using RegEx.Match  how would I get that part.

    Wednesday, February 19, 2020 8:51 PM

All replies

  • RegEx Lite is a great VS extension you can install to play around with REs and get the expression you need. Here's the expression I came up with assuming the string can only appear once in each string you are parsing.

    .*\$(?<value>.*)\$.*

    After calling the Match method you can get the value from the `value` group.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, February 19, 2020 9:49 PM
    Moderator
  • The regular expression is reasonably simple; the only precaution is that you have to escape the $ sign because it has a special meaning in regular expressions:

    string pattern = @"\$.*\$";

    You then use the "Matches" method in Regex:

    MatchCollection matches = Regex.Match(stringToParse, pattern);

    And then matches[0].Value will contain the expected segment. Remember to check the length of matches in case it has failed to find anything.

    Wednesday, February 19, 2020 9:49 PM
    Moderator
  • Hi William,

    Thank you for posting here.

    Alberto's idea is basically correct, but there is a small mistake. When using Regex.Match (), the return type should be Match. When using Regex.Matches (), the return type is MatchCollection.

    He mentioned this, but accidentally wrote it wrong.

    Moreover, because the matching start and end symbols are both $, if two matching conditions appear in a string, such as "xyz,00\r\rSometext\r\n$TextIneed 5.5$\0$TextIneed 4$", This method will return" $ TextIneed 5.5 $ \ 0 $ TextIneed 4 $ ", so please let us know if this will happen, if not, please adopt Alberto's answer and mark it as answer, if yes, please tell us that we will come up with more appropriate answers.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 20, 2020 6:35 AM
  • Hi,

    Has your problem been solved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 26, 2020 9:38 AM