Regex Expression to extract everything following a string and before a space
-
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
-
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
-
Perhaps with a couple of extra
\d
s 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
-
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
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
-
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
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
-
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
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
-
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
-
Perhaps with a couple of extra
\d
s 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
-
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
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[^]. -
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
You specifically said you wanted to extract the text up to the first space, and there is a space between
2017
and10:54
. And then another space between10:54
andAM
. 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
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You specifically said you wanted to extract the text up to the first space, and there is a space between
2017
and10:54
. And then another space between10:54
andAM
. 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
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
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
-
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[^].Yes, that work! Thanks
-
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
-
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
-
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
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()));
-
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
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