none
Trying to format a string RRS feed

  • Question

  • I believe with how I'm looking to format my string I'm looking to use regex but I'm open to ideas and know nothing about it and so I'm looking for someone to provide if they agree a way to learn them or an answer I'm truly open to either in the case of regex. I want to mention this is also WFA not Command Prompt (CLI)

    For example sake this is how my strings may vary:

    This This Is This Is John This Is John Doe This Is John Doe Jane This Is John Doe Jane Doe

    This is what I need to convert those too:

    this
    thisIs
    thisIs_john
    thisIs_johnDoe
    thisIs_johnDoe_jane
    thisIs_johnDoe_janeDoe

    *Examples may vary names may change completely. The examples provided are just examples so please don't expect the same letters will always be used. It could be That Was Shane Dawson Swimming. I do know the inputs but they have a standard formatting to them as well as a standard formatting I need them translated to. 

    Most everything I'm working with is between This Is and This Is John Doe I just put the rest in to show how it would continue to format over and over from the end and shortest one is just to show regardless I'd need first character lowercase.



    • Edited by fatalerror0x00 Tuesday, July 3, 2018 4:30 PM Missing information
    Tuesday, July 3, 2018 3:16 AM

All replies

  • Try LINQ too:

     

    string example = "This Is John Doe Jane Doe";

     

    string result = string.Join( "_",

                                 example

                                    .Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries )

                                    .Select( ( w, i ) => new { w = i % 2 == 0 ? w.ToLower() : w, i = i / 2 } )

                                    .GroupBy( p => p.i, p => p.w )

                                    .Select( w => string.Join( "", w ) ) );

     

    Console.WriteLine( example );

    Console.WriteLine( result );


    Tuesday, July 3, 2018 6:28 AM
  •    

    Hi fatalerror0x00,

    Thank you for posting here.

    For your question Trying to format a string, I suggest to try the code below.

    static void Main(string[] args)
            {
                string str = "This Is Jhon Doe Jane Doe Shane Dawson Swimming";
                string[] strs = str.Split(' ');
                for (int i = 0; i < strs.Length; i++)
                {
                    if (i == 0) strs[i] = strs[i].ToLower();
                    else
                    if (i % 2 == 0) strs[i] = "_" + strs[i].ToLower();
                    Console.Write(strs[i]);
                }
                Console.WriteLine();
            }
    

    Best regards,

    Zhanglong


    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, July 3, 2018 7:57 AM
    Moderator
  • I love your inconsistency of spelling John :P all kidding aside I need to update my original post since I guess being tired I left out some details though some not as important as others. So Check that out:)
    Tuesday, July 3, 2018 4:17 PM
  • REs could work but it completely depends upon your grammar rules and I'm afraid you have an ambiguity in yours. The general format seems to be: This Is ([firstname] [lastname])*

    Given that you are camel casing each part, separating them by underscore. But a single value could go different ways.

    This Is John Doe

    firstname = John, lastname = Doe

    firstname = John, lastname = , firstname = Doe, lastname = 

    This is John Doe Jane is ambiguous as well.

    firstname = John, lastname = Doe, firstname = Jane, lastname =

    firstname = John, lastname = , firstname = Doe, lastname = Jane

    You would need to define some rules such as only the final name pattern can have just a first name. In that case REs become a little more doable. This is a starter for just the patterns you gave.

    (?<start>This\s+is)(\s+(?<name>\w+(\s+\w+)?))*

    The first half is in the "start" capture group while the name(s) are in the "name" capture group. You can enumerate them and strip/camel case each set. You can then join them using an underscore to produce the final string.

    Note that this seems to be in the context of text recognition. If you're trying to do that then simple REs aren't going to work. You should look into a recognition engine instead. Your last example wouldn't work the aforementioned REs because it requires semantic parsing which isn't possible in an RE (or any scanner for that matter). 


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 3, 2018 5:31 PM
    Moderator