I am trying to extract a date from a string using regex in Python 3 (using Anaconda &amp; Jupyter Notebooks on Win10).  This seems like a simple task but the pattern I'm trying to find doesn't seem to want to extract the date properly using the string that I need to work with.  The pattern appears to work properly for other strings however.  Here's my code:  # Trying to extract this text sequence from the test string: 'April 2, 2020 at 12:30PM'. import re  test = 'Hampden\n \nUnknown\n \nYes\n \nFemale\n \n30s\n \nSuffolk\n \nYes\n \nYes\n  \n\nPAGE2\nMASSACHUSETTS DEPARTMENT OF PUBLIC HEALTH\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n  \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \nData are cumulative and current as of  \nApril \n2\n, 2020 at 12:30PM.\n \n*Other commercial and clinical laboratories continue to come on  line. As laboratory testing results are \nprocessed and the source verified, they will be integrated  into this daily report\n.\n \nL\naboratory\n \nTotal\n \nPatients \nPositive\n \nTotal Patients  Tested\n \nMA State Public Health Laboratory\n \n912\n \n7355\n \nARUP\n \nLaboratories\n \n17\n  \n187\n \nBaystate Medical Center\n \n5\n \n12\n \nBedford \nResearch Foundation\n \n30\n \n219\n  \nBeth Israel Deaconess Medical Center\n \n686\n \n3976\n \nBioReference Laboratories\n \n9\n \n43\n  \nBoston Medical Center\n \n236\n \n647\n \nBROAD Institute CRSP\n \n348\n \n1784\n \nCenters for  Disease Control and Prevention\n \n1\n \n11\n \n\n \n11\n \n195\n \nGenesys Diagnostics\n \n106\n  \n145\n \nLabCorp\n \n715\n \n4836\n \nMayo Clinic Labs\n \n353\n \n1812\n \nPartners Healthcare\n  \n740\n \n4112\n \nQuest Laboratories\n \n4161\n \n27606\n \nSouth Shore Hospital\n \n7\n \n34\n  \nTufts Medical Center\n \n463\n \n2154\n \nUMASS Memorial Medical Center\n \n32\n \n163\n  \nViracor\n \n80\n \n1150\n \nOther\n \n54\n \n167\n \nTotal\n \nPatients Tested*\n \n8966\n  \n56608\n \n'  newtest = test.replace("\n", "") result1 = re.findall(r"Data are cumulative and current as of (.+)\.", newtest, re.IGNORECASE) print(result1)  # This returns "April 2, 2020 at 12:30PM. *Other commercial and clinical laboratories continue to  # come on line. As  # laboratory testing results are processed and the source verified, they will be integrated into this  # daily report". # Why does it not find the . after the PM and stop extracting?  # When I attempt to run the pattern again on result1, again, it doesn't seem to find the . after the  # PM (it extracts until the next period) and # returns: "April 2, 2020 at 12:30PM. *Other commercial and clinical laboratories continue to come on  # line". # What is special about that . after PM that keeps regex from seeing it??? result2 = re.findall(r"(.+)\.", result1[0], re.IGNORECASE) print(result2)  # If I use the same pattern on a subset of the test string, it sees the . after the PM and returns # the correct result.  Why??? subtest = 'Data are cumulative and current as of April 2, 2020 at 12:30PM. *Other commercial' result3 = re.findall(r"Data are cumulative and current as of (.+)\.", subtest, re.IGNORECASE) print(result3)   I'm guessing I must be doing something stupid but I haven't been able to figure out what it is.  Can anyone provide some guidance?  I could change the pattern to this (r"Data are cumulative and current as of (.+[P|A]M)") and it works properly on all strings but would still like to know why it seems to be having problems seeing the . after the PM.  Appreciate any help.
3211
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM J0000010: Project name: E:\foo.pf J0000011: Job name: MBiek Direct Mail Test J0000020: Document 1 - Completed successfully  I'm trying to extract pieces from it using regex. In this case, I want to grab everything after Project Name up to the part where it says J0000011: (the 11 is going to be a different number every time). Here's the regex I've been playing with: Project name:\s+(.*)\s+J[0-9]{7}:  The problem is that it doesn't stop until it hits the J0000020: at the end. How do I make the regex stop at the first occurrence of J[0-9]{7}?
19052
Is it possible to use a RegEx to validate, or sanitize Base64 data?  That's the simple question, but the factors that drive this question are what make it difficult.  I have a Base64 decoder that can not fully rely on the input data to follow the RFC specs.  So, the issues I face are issues like perhaps Base64 data that may not be broken up into 78 (I think it's 78, I'd have to double check the RFC, so don't ding me if the exact number is wrong) character lines, or that the lines may not end in CRLF; in that it may have only a CR, or LF, or maybe neither.  So, I've had a hell of a time parsing Base64 data formatted as such.  Due to this, examples like the following become impossible to decode reliably.  I will only display partial MIME headers for brevity.  Content-Transfer-Encoding: base64  VGhpcyBpcyBzaW1wbGUgQVNDSUkgQmFzZTY0IGZvciBTdGFja092ZXJmbG93IGV4YW1wbGUu   Ok, so parsing that is no problem, and is exactly the result we would expect.    And in 99% of the cases, using any code to at least verify that each char in the buffer is a valid base64 char, works perfectly.  But, the next example throws a wrench into the mix.  Content-Transfer-Encoding: base64  http://www.stackoverflow.com VGhpcyBpcyBzaW1wbGUgQVNDSUkgQmFzZTY0IGZvciBTdGFja092ZXJmbG93IGV4YW1wbGUu   This a version of Base64 encoding that I have seen in some viruses and other things that attempt to take advantage of some mail readers desire to parse mime at all costs, versus ones that go strictly by the book, or rather RFC; if you will.  My Base64 decoder decodes the second example to the following data stream.  And keep in mind here, the original stream is all ASCII data!  [0x]86DB69FFFC30C2CB5A724A2F7AB7E5A307289951A1A5CC81A5CC81CDA5B5C1B19481054D0D 2524810985CD94D8D08199BDC8814DD1858DAD3DD995C999B1BDDC8195E1B585C1B194B8   Anyone have a good way to solve both problems at once?  I'm not sure it's even possible, outside of doing two transforms on the data with different rules applied, and comparing the results.  However if you took that approach, which output do you trust?  It seems that ASCII heuristics is about the best solution, but how much more code, execution time, and complexity would that add to something as complicated as a virus scanner, which this code is actually involved in?  How would you train the heuristics engine to learn what is acceptable Base64, and what isn't?    UPDATE:  Do to the number of views this question continues to get, I've decided to post the simple RegEx that I've been using in a C# application for 3 years now, with hundreds of thousands of transactions.  Honestly, I like the answer given by  the best, which is why I picked it as the selected answer.  But to anyone using C#, and looking for a very quick way to at least detect whether a string, or byte[] contains valid Base64 data or not, I've found the following to work very well for me.  [^-A-Za-z0-9+/=]|=[^=]|={3,}$   And yes, this is just for a STRING of Base64 data, NOT a properly formatted  message.  So, if you are dealing with data of this type, please take that into account before attempting to use the above RegEx.  If you are dealing with Base16, Base32, Radix or even Base64 for other purposes (URLs, file names, XML Encoding, etc.), then it is highly recommend that you read  that  mentioned in his answer as you need to be well aware of the charset and terminators used by the implementation before attempting to use the suggestions in this question/answer set.
59657
Regex.IsMatch( "foo", "[\U00010000-\U0010FFFF]" )    Throws: System.ArgumentException: parsing "[-]" - [x-y] range in reverse order.  Looking at the hex values for \U00010000 and \U0010FFF I get: 0xd800 0xdc00 for the  first character and 0xdbff 0xdfff for the second.  So I guess I have really have one problem. Why are the Unicode characters formed with \U split into two chars in the string?
18973
I need to write a function that receives a string and a regex. I need to check if there is a match and return the start and end location of a match. (The regex was already compiled by qr//.)  The function might also receive a "global" flag and then I need to return the (start,end) pairs of all the matches.  I cannot change the regex, not even add () around it as the user might use () and \1. Maybe I can use (?:).  Example: given "ababab" and the regex qr/ab/, in the global case I need to get back 3 pairs of (start, end).
107508
I am working on a form widget for users to enter a time of day into a text input (for a calendar application). Using JavaScript (we are using jQuery FWIW), I want to find the best way to parse the text that the user enters into a JavaScript Date() object so I can easily perform comparisons and other things on it.  I tried the parse() method and it is a little too picky for my needs. I would expect it to be able to successfully parse the following example input times (in addition to other logically similar time formats) as the same Date() object:   1:00 pm 1:00 p.m. 1:00 p 1:00pm 1:00p.m. 1:00p 1 pm 1 p.m. 1 p 1pm 1p.m. 1p 13:00 13   I am thinking that I might use regular expressions to split up the input and extract the information I want to use to create my Date() object. What is the best way to do this?
16097
I wrote a Javascript RegExp test to detect date string format, I added an redundant "g" flag by mistake and found something interesting.  var s = "2009/03/10"; var regex=/^\d{4}[/]\d{2}[/]\d{2}$/g; alert(regex.test(s)); alert(regex.test(s)); alert(regex.test(s)); alert(regex.test(s));   I got a 'true' followed by a 'false', then another 'true', then another 'false'.  If I use a loop to execute it, I found something more interesting, I got four "true" in IE and Safari, and true,false,true,false in FF, Chrome.  for (var i=0; i&lt;4; i++) {   var s = "2009/03/10";   var regex=/^\d{4}[/]\d{2}[/]\d{2}$/g;   alert(regex.test(s)); }   Does anybody has idea to explain why the Javascript regex behaves like that and what cause browsers return different results? ( related to variable declaration and life scope? )
92750
I have a Perl regular expression (shown , though understanding the whole thing isn't hopefully necessary to answering this question) that contains the \G metacharacter. I'd like to translate it into Python, but Python doesn't appear to support \G. What can I do?
262
I don't really understand regular expressions.  Can you explain them to me in an easy-to-follow manner?  If there are any online tools or books, could you also link to them?
29424
In a batch file I need to extract a month, day, year from the date command.  So I used the following, which essentially parses the Date command to extract its sub strings into a variable:  set Day=%Date:~3,2% set Mth=%Date:~0,2% set Yr=%Date:~6,4%   This is all great, but if I deploy this batch file to a machine with a different regional/country settings, it fails because month, day and year are in different locations.   How can I extract month, day and year regardless of the date format?
99429
Is there a fairly easy way to convert a datetime object into an RFC 1123 (HTTP/1.1) date/time string, i.e. a string with the format  Sun, 06 Nov 1994 08:49:37 GMT   Using strftime does not work, since the strings are locale-dependant. Do I have to build the string by hand?
118716
awk -F&quot;,&quot; '{OFS=&quot;,&quot;; $1=system(&quot;date -d &quot;$1&quot; +%d-%m-%Y&quot;) ; print $0}' data.csv | head   I am trying to take the first column of a csv file and standardize the date format to %dd-%mm-%yyyy. When I try the code above I get date: write error: Broken pipe. But when I try the code below: dd=$(csvcut -c  1 -e ISO-8859-1 -d &quot;,&quot;  data.csv | head -2 | sed -n 2p) echo $dd echo $(date -d $dd &quot;+%d-%m-%Y&quot;)  I get the the output:  2017-02-03 03-02-2017  What am I doing wrong. Any hints? Thanks. My system: Ubuntu 16.04.1 LTS EDIT 2: Here is a sample file:  EDIT: sure CSV data file (input): data.csv (with many rows) Bogført,Tekst,Beløb,Saldo    2017-02-03, random text,-425,-611524.54    output: Bogført,Tekst,Beløb,Saldo     03-02-2017, random text,-425,-611524.54   but the date format can be in any other format. I am currently looking into standardizing dates for an csv import job. Thanks.
6955
I'm reading text from a flat file in c# and need to test whether certain values are dates. They could be in either YYYYMMDD format or MM/DD/YY format. What is the simplest way to do this in .Net?
70561
I have a text file:  deiauk 1611516 afsdf 765 minkra 18415151 asdsf 4152 linkra sfsfdsfs sdfss 4555 deiauk1 sdfsfdsfs 1561 51 deiauk2 115151 5454 4 deiauk 1611516 afsdf ddfgfgd luktol1 4545 4 9 luktol 1   and I want to match exactly deiauk.  When I do this:  grep "deiauk" file.txt   I get this result:  deiauk 1611516 afsdf 765 deiauk1 sdfsfdsfs 1561 51 deiauk2 115151 5454 4   but I only need this:  deiauk 1611516 afsdf 765 deiauk 1611516 afsdf ddfgfgd   I know there's a -w option, but then my string has to mach whole line.
62549
I'm having a bit of trouble getting a Python regex to work when matching against text that spans multiple lines. The example text is ('\n' is a newline)  some Varying TEXT\n \n DSJFKDAFJKDAFJDSAKFJADSFLKDLAFKDSAF\n [more of the above, ending with a newline]\n [yep, there is a variable number of lines here]\n \n (repeat the above a few hundred times).   I'd like to capture two things: the 'some_Varying_TEXT' part, and all of the lines of uppercase text that comes two lines below it in one capture (i can strip out the newline characters later).  I've tried with a few approaches:  re.compile(r"^&gt;(\w+)$$([.$]+)^$", re.MULTILINE) # try to capture both parts re.compile(r"(^[^&gt;][\w\s]+)$", re.MULTILINE|re.DOTALL) # just textlines   and a lot of variations hereof with no luck. The last one seems to match the lines of text one by one, which is not what I really want. I can catch the first part, no problem, but I can't seem to catch the 4-5 lines of uppercase text. I'd like match.group(1) to be some&#95;Varying&#95;Text and group(2) to be line1+line2+line3+etc until the empty line is encountered.  If anyone's curious, its supposed to be a sequence of aminoacids that make up a protein.
3154
I have a string User name (sales) and I want to extract the text between the brackets, how would I do this?   I suspect sub-string but I can't work out how to read until the closing bracket, the length of text will vary.
4419
I need to delete some Unicode symbols from the string 'بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ'  I know they exist here for sure. I tried:  re.sub('([\u064B-\u0652\u06D4\u0670\u0674\u06D5-\u06ED]+)', '', 'بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ')   but it doesn't work. String stays the same. What am I doing wrong?
37932
Does anyone happen to know if there is a token I can add to my csv for a certain field so Excel doesn't try to convert it to a date?  I'm trying to write a .csv file from my application and one of the values happens to look enough like a date that Excel is automatically converting it from text to a date.  I've tried putting all of my text fields (including the one that looks like a date) within double quotes, but that has no effect.