locked
Match pattern and replace in string RRS feed

  • Question

  • Hi,

    I have a string in which there is particular pattern I need to find and replace by blank, Can someone please advise

    eg:

    String 1: 'CAM# 989898 This is refrigerator'

    String 2 : 'shadows for blinds CAM# 8765400 of the bridge'

    string 3: 'does not have pattern'

    So i want to look for pattern CAM# XXXXXX (6 numeric digits) and replace it by blank.

    I would appreciate any help.

    Thanks

    Sunday, December 22, 2019 10:56 PM

Answers

  • Hi,

    I'd like to provide an algorithm.
    (1) get position of 'CAM# ' (with one space after #, 5 characters) -- IndexOf method
        (if the result is -1, 'CAM# ' is not found)
    (2) (when found) get 12 characters from the start position of 'CAM# ' -- SubString method
        (12 characters = 5 + 7)
    (3) if (characters from 6th to 11th are all numeric) and (the 12th character is space), that is matched.
       (when matched) replace characters from 6th to 11 with space.

    [note]
      If there is a possibility that 6 numeric digits are placed the end of a string, it is needed to change (2) and (3).
    Regards,

    Ashidacchi -- http://hokusosha.com

    Monday, December 23, 2019 12:03 AM
  • Hi SA260884,

    Thank you for posting here.

    My idea is basically the same as Ashidacchi.

    Here is the code.

            static List<string> DoWork(List<string> list) 
            {
                List<string> result = new List<string>();
    
                foreach (var item in list)
                {
                    int index = item.IndexOf("CAM#");
    
                    if (index != -1)
                    {
                        string str = item.Substring(index, 11);
                        string res = item.Replace(str, null);
                        result.Add(res);
                    }
                    else
                    {
                        result.Add(item);
                    }
                }
                return result;
            }

    Result:

    Hope this could be helpful.

    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.

    Monday, December 23, 2019 2:19 AM
  • Check this sample too:

    string text = @"

    CAM# 989898 This is refrigerator

    shadows for blinds CAM# 876540 of the bridge

    does not have pattern";

     

    string result = Regex.Replace( text, @"\bCAM# \d{6}(?!\d)", "" );

     

    Values like “Cam# 989898”, “CAM#989898”, “XCAM# 989898” and “CAM# 000989898” will not be deleted.


    Monday, December 23, 2019 5:46 AM

All replies

  • Hi,

    I'd like to provide an algorithm.
    (1) get position of 'CAM# ' (with one space after #, 5 characters) -- IndexOf method
        (if the result is -1, 'CAM# ' is not found)
    (2) (when found) get 12 characters from the start position of 'CAM# ' -- SubString method
        (12 characters = 5 + 7)
    (3) if (characters from 6th to 11th are all numeric) and (the 12th character is space), that is matched.
       (when matched) replace characters from 6th to 11 with space.

    [note]
      If there is a possibility that 6 numeric digits are placed the end of a string, it is needed to change (2) and (3).
    Regards,

    Ashidacchi -- http://hokusosha.com

    Monday, December 23, 2019 12:03 AM
  • Hi SA260884,

    Thank you for posting here.

    My idea is basically the same as Ashidacchi.

    Here is the code.

            static List<string> DoWork(List<string> list) 
            {
                List<string> result = new List<string>();
    
                foreach (var item in list)
                {
                    int index = item.IndexOf("CAM#");
    
                    if (index != -1)
                    {
                        string str = item.Substring(index, 11);
                        string res = item.Replace(str, null);
                        result.Add(res);
                    }
                    else
                    {
                        result.Add(item);
                    }
                }
                return result;
            }

    Result:

    Hope this could be helpful.

    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.

    Monday, December 23, 2019 2:19 AM
  • Check this sample too:

    string text = @"

    CAM# 989898 This is refrigerator

    shadows for blinds CAM# 876540 of the bridge

    does not have pattern";

     

    string result = Regex.Replace( text, @"\bCAM# \d{6}(?!\d)", "" );

     

    Values like “Cam# 989898”, “CAM#989898”, “XCAM# 989898” and “CAM# 000989898” will not be deleted.


    Monday, December 23, 2019 5:46 AM
  • 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 the 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.

    Tuesday, January 7, 2020 9:06 AM