Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. Regex Expression to extract everything following a string and before a space

Regex Expression to extract everything following a string and before a space

Scheduled Pinned Locked Moved Regular Expressions
regextutorial
16 Posts 4 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14572415
    wrote on last edited by
    #1

    Hi all, I am looking for a regular expression to extract everything following a string and before a space. Please see the example below. Order placed at the store. Order Date: 01/12/2017 This was completed. From the above string, I would like to extract the following. Order Date: 01/12/2017 Thanks, Ana

    L Richard DeemingR 2 Replies Last reply
    0
    • M Member_14572415

      Hi all, I am looking for a regular expression to extract everything following a string and before a space. Please see the example below. Order placed at the store. Order Date: 01/12/2017 This was completed. From the above string, I would like to extract the following. Order Date: 01/12/2017 Thanks, Ana

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      How about: Order Date: \d\d\/\d\d\/\d\d

      Richard DeemingR M 2 Replies Last reply
      0
      • L Lost User

        How about: Order Date: \d\d\/\d\d\/\d\d

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Perhaps with a couple of extra \ds to cope with the four-digit year? :)


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        L 1 Reply Last reply
        0
        • M Member_14572415

          Hi all, I am looking for a regular expression to extract everything following a string and before a space. Please see the example below. Order placed at the store. Order Date: 01/12/2017 This was completed. From the above string, I would like to extract the following. Order Date: 01/12/2017 Thanks, Ana

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          If you want to include the "Order Date: " in your extracted value:

          Order Date:\s*[^\s]+

          If you just want the "01/12/2017", and you're using C#, then:

          (?<=Order Date:\s*)[^\s]+

          (Other regular expression processors may also support "zero-width positive lookbehind" assertions. But Javascript, for example, doesn't.)


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          M 2 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            If you want to include the "Order Date: " in your extracted value:

            Order Date:\s*[^\s]+

            If you just want the "01/12/2017", and you're using C#, then:

            (?<=Order Date:\s*)[^\s]+

            (Other regular expression processors may also support "zero-width positive lookbehind" assertions. But Javascript, for example, doesn't.)


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            M Offline
            M Offline
            Member_14572415
            wrote on last edited by
            #5

            Hi Richard, Thanks for your response! Since I need the order date, I tried using "Order Date:\s*[^\s]+" per your post above, and it is pulling text after the date, time and AM/PM. So, I would like to pull everything before the date, time and AM/PM. Any suggestions would be appreciated. Thank you! Ana

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              If you want to include the "Order Date: " in your extracted value:

              Order Date:\s*[^\s]+

              If you just want the "01/12/2017", and you're using C#, then:

              (?<=Order Date:\s*)[^\s]+

              (Other regular expression processors may also support "zero-width positive lookbehind" assertions. But Javascript, for example, doesn't.)


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              M Offline
              M Offline
              Member_14572415
              wrote on last edited by
              #6

              So, my string is like the following. Order placed at the store. Order Date: 01/12/2017 10:54 AM This was completed. I would like to pull "Order Date: 01/12/2017 10:54 AM" Hope this helps. Thanks, Ana

              Richard DeemingR 1 Reply Last reply
              0
              • L Lost User

                How about: Order Date: \d\d\/\d\d\/\d\d

                M Offline
                M Offline
                Member_14572415
                wrote on last edited by
                #7

                Hi Richard, My string is like the following. Order placed at the store. Order Date: 01/12/2017 10:54 AM This was completed. I would like to pull "Order Date: 01/12/2017 10:54 AM" Hope this helps. Thanks, Ana

                L 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Perhaps with a couple of extra \ds to cope with the four-digit year? :)


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Oops again. :doh:

                  1 Reply Last reply
                  0
                  • M Member_14572415

                    Hi Richard, My string is like the following. Order placed at the store. Order Date: 01/12/2017 10:54 AM This was completed. I would like to pull "Order Date: 01/12/2017 10:54 AM" Hope this helps. Thanks, Ana

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    If you take my original Order Date: \d\d\/\d\d\/\d\d , plus Richard Deeming's reply to me \d\d for the rest of the year, then you should be able to work out what else is needed. And you can test your expression online at RegExr: Learn, Build, & Test RegEx[^].

                    M 1 Reply Last reply
                    0
                    • M Member_14572415

                      So, my string is like the following. Order placed at the store. Order Date: 01/12/2017 10:54 AM This was completed. I would like to pull "Order Date: 01/12/2017 10:54 AM" Hope this helps. Thanks, Ana

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      You specifically said you wanted to extract the text up to the first space, and there is a space between 2017 and 10:54. And then another space between 10:54 and AM. If the date and time is always in the same format - which doesn't match the example you gave in your original message - then try something like:

                      Order Date: \d\d\/\d\d\/\d\d\d\d \d\d:\d\d [AP]M

                      Demo[^]


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      M 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        You specifically said you wanted to extract the text up to the first space, and there is a space between 2017 and 10:54. And then another space between 10:54 and AM. If the date and time is always in the same format - which doesn't match the example you gave in your original message - then try something like:

                        Order Date: \d\d\/\d\d\/\d\d\d\d \d\d:\d\d [AP]M

                        Demo[^]


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        M Offline
                        M Offline
                        Member_14572415
                        wrote on last edited by
                        #11

                        Hi Richard, Sorry for the confusion. I tried the expression that you posted and it works perfectly! Also, I would like to pull "Name is: Tia" from the following string: My friend's Name is: Tia She lives in Argentina. Thanks so much, Ana

                        Richard DeemingR 1 Reply Last reply
                        0
                        • L Lost User

                          If you take my original Order Date: \d\d\/\d\d\/\d\d , plus Richard Deeming's reply to me \d\d for the rest of the year, then you should be able to work out what else is needed. And you can test your expression online at RegExr: Learn, Build, & Test RegEx[^].

                          M Offline
                          M Offline
                          Member_14572415
                          wrote on last edited by
                          #12

                          Yes, that work! Thanks

                          1 Reply Last reply
                          0
                          • M Member_14572415

                            Hi Richard, Sorry for the confusion. I tried the expression that you posted and it works perfectly! Also, I would like to pull "Name is: Tia" from the following string: My friend's Name is: Tia She lives in Argentina. Thanks so much, Ana

                            Richard DeemingR Offline
                            Richard DeemingR Offline
                            Richard Deeming
                            wrote on last edited by
                            #13

                            Only one name, with no spaces?

                            Name is: [^\s]+

                            Demo[^]


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                            M 1 Reply Last reply
                            0
                            • Richard DeemingR Richard Deeming

                              Only one name, with no spaces?

                              Name is: [^\s]+

                              Demo[^]


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              M Offline
                              M Offline
                              Member_14572415
                              wrote on last edited by
                              #14

                              Hi, I tried "Name is: [^\s]+" but I am getting back the text after the space as well. How can I fix this? Thanks, Ana

                              P Richard DeemingR 2 Replies Last reply
                              0
                              • M Member_14572415

                                Hi, I tried "Name is: [^\s]+" but I am getting back the text after the space as well. How can I fix this? Thanks, Ana

                                P Offline
                                P Offline
                                phil o
                                wrote on last edited by
                                #15

                                You could try "Name is:\s([^\s]+).*$". The result will be in the first numbered group of the result.

                                string input = "My friend's Name is: Tia She lives in Argentina.";
                                Regex r = new Regex(@"Name is:\s([^\s]+).*$");
                                string name = r.Match(input).Groups[0].Value;

                                while (!(success = Try()));

                                1 Reply Last reply
                                0
                                • M Member_14572415

                                  Hi, I tried "Name is: [^\s]+" but I am getting back the text after the space as well. How can I fix this? Thanks, Ana

                                  Richard DeemingR Offline
                                  Richard DeemingR Offline
                                  Richard Deeming
                                  wrote on last edited by
                                  #16

                                  Then there's either a bug with the regular expression engine you're using, or there's something wrong with your code. [^\s]+ explicitly excludes any spaces. That pattern will match the first run of characters which are not spaces to appear after the prefix "Name is: ".


                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups