none
C# Regex - remove every chars after the "REF" sequence RRS feed

  • Question

  • Hi,

    I need a C# Regex to clean a string and remove every thing after the "REF" word

    ex: "SKU12AS4REF123 should become SKU12AS4

    Many thanks


    Thursday, April 18, 2019 1:00 PM

Answers

  • Hi

    Thank you for posting here.

    Based on your description, you want to clean a string and remove everything after the "REF" word by using a c# Regex.

    You could try the following code.

      
               string line = "SKU12AS4REF123";
    
                Regex reg = new Regex("(.+)REF");
    
                Match match = reg.Match(line);
    
                string value2 = match.Groups[1].Value;
    
               Console.WriteLine(value2);

    Result:

    Best regards,

    Jack


    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.


    Friday, April 19, 2019 6:05 AM
    Moderator

All replies

  • Does it need to be a RegEx? You can use a simple IndexOf/Substring:

    string test = "SKU12AS4REF123";
    
    string result = test.Substring(0, test.IndexOf("REF"));
    


    Thursday, April 18, 2019 1:17 PM
    Moderator
  • Hello,

    Please work on one question at a time, once resolved start a new question.

    For example I replied to the following thread, is it resolved? If so mark it as answered or reply back if not resolved.

    https://social.msdn.microsoft.com/Forums/en-US/8d3f792a-bb7f-44dc-a6f7-ddbfc8b3d1ca/find-string-between-?forum=csharpgeneral


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 18, 2019 1:22 PM
    Moderator
  • Thanks Alberto,

    I'm using that way after a regex to remove other chars (^\d\w  and other specific words)

    I was trying to obtainone single regex to clean it all and get a better control of the regex technics

    Thursday, April 18, 2019 7:52 PM
  • Hi

    Thank you for posting here.

    Based on your description, you want to clean a string and remove everything after the "REF" word by using a c# Regex.

    You could try the following code.

      
               string line = "SKU12AS4REF123";
    
                Regex reg = new Regex("(.+)REF");
    
                Match match = reg.Match(line);
    
                string value2 = match.Groups[1].Value;
    
               Console.WriteLine(value2);

    Result:

    Best regards,

    Jack


    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.


    Friday, April 19, 2019 6:05 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "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,

    Jack


    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.

    Monday, April 22, 2019 3:25 AM
    Moderator