none
Regular Expressions (parse string and move string within same string) RRS feed

  • Question

  • I am a newbie at this and I am trying to build an regular expression to parse a string examples follow…

    Example:

    Random text of varying length may contain non text (123) charters (apples) more random text

    In the above string, I need to find “(apples)”and move it to the end of the same string.

    The desired result: ($1 $3 $2 - I presume after proper parsing)

    Random text of varying length may contain non text (123) charters more random text (apples)

    The text to move to the end of the string is always “(apples)” without the quotes.

    Here are more examples:

    • This is my string (apples) (the rest of my string) à This is my string (the rest of my string) (apples)
    • Mr. String 3 21 (apples) more string (more string) à Mr. String 3 21 more string (more string) (apples)
    • String (apples) string à String string (apples)

    Can this be done with Regular Expressions?

    Thanks,

    Kevin

    PS: Please forgive me is this i not in the proper forum.

    PPS: If this is not the proper forum please point me to the proper forum. - Thanks


    • Edited by kweiner1960 Friday, August 29, 2014 8:48 PM typo
    Friday, August 29, 2014 8:45 PM

Answers

  • Hello, In this case, why using regexp ? You could do that with string object. String str =" my string (apples) is here"; String tmpstr = str.Remove(str.IndexOf("(apples)",8)); string tmp2 = tmpstr.insert(tmpstr.Length-1,"(apples")); Regards Cedric
    Saturday, August 30, 2014 5:49 AM
  • Try this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static string[] inputs = {
                                 "Random text of varying length may contain non text (123) charters (apples) more random text",
                                 "This is my string (apples) (the rest of my string)",
                                 "Mr. String 3 21 (apples) more string (more string)",
                                 "String (apples) string "
            };
            static void Main(string[] args)
            {
                string pattern = @"\([^\)]*\)";
                foreach (string input in inputs)
                {
                    MatchCollection matches = Regex.Matches(input, pattern);
                    string suffix = "";
                    foreach (Match match in matches)
                    {
                        suffix = match.ToString() + suffix;
                    }
                    string result = Regex.Replace(input, pattern, "") + suffix;
                    Console.WriteLine(result);
                }
                Console.ReadLine();
            }
        }
    }
    


    jdweng

    Saturday, August 30, 2014 3:48 PM
  • If you are still interested in Regular Expressions, then try this too:

    string output = Regex.Replace( input, @"(.*)([(]apples[)]\s+)(.*)", "$1$3 $2" );

    Saturday, August 30, 2014 4:22 PM

All replies

  • Hello, In this case, why using regexp ? You could do that with string object. String str =" my string (apples) is here"; String tmpstr = str.Remove(str.IndexOf("(apples)",8)); string tmp2 = tmpstr.insert(tmpstr.Length-1,"(apples")); Regards Cedric
    Saturday, August 30, 2014 5:49 AM
  • Try this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static string[] inputs = {
                                 "Random text of varying length may contain non text (123) charters (apples) more random text",
                                 "This is my string (apples) (the rest of my string)",
                                 "Mr. String 3 21 (apples) more string (more string)",
                                 "String (apples) string "
            };
            static void Main(string[] args)
            {
                string pattern = @"\([^\)]*\)";
                foreach (string input in inputs)
                {
                    MatchCollection matches = Regex.Matches(input, pattern);
                    string suffix = "";
                    foreach (Match match in matches)
                    {
                        suffix = match.ToString() + suffix;
                    }
                    string result = Regex.Replace(input, pattern, "") + suffix;
                    Console.WriteLine(result);
                }
                Console.ReadLine();
            }
        }
    }
    


    jdweng

    Saturday, August 30, 2014 3:48 PM
  • If you are still interested in Regular Expressions, then try this too:

    string output = Regex.Replace( input, @"(.*)([(]apples[)]\s+)(.*)", "$1$3 $2" );

    Saturday, August 30, 2014 4:22 PM
  • Cedric – Thanks! I had (have) tunnel vision it never occurred to me.

    Jdweng – Thank you for taking the time to show this solution.

    Viorel_ - Thank you. I must lean Regular Expressions. It amazes me to see how graceful and tight a Regular Expression can be. Mind boggling to lean the syntax, but graceful and efficient in the end.

    Kevin

    Tuesday, September 2, 2014 7:40 PM