I am new to regular expressions in python. I want to split strings based on delimiters. But the delimiters should also be returned as elements. Such as  Input - "The man.in the. high castle."  Output - ["The", "man", ".", "in", "the", ".", "high", "castle", "."]   or  Input - "The man-in the. high castle!" Output - ["The", "man", "-", "in", "the", ".", "high", "castle", "!"]   Please note that I am not only trying to split sentences based on white space, delimiters but also return the delimiters. This is somewhat similar to nltk.word_tokenize, but different because word_tokenize cannot split words such as "man.in".   My set of delimiters are ".","?","!", "," and whitespace Thanks in advance
11923
I'm trying to split a string up into words and punctuation, adding the punctuation to the list produced by the split.  For instance:  &gt;&gt;&gt; c = "help, me" &gt;&gt;&gt; print c.split() ['help,', 'me']   What I really want the list to look like is:  ['help', ',', 'me']   So, I want the string split at whitespace with the punctuation split from the words.  I've tried to parse the string first and then run the split:  &gt;&gt;&gt; for character in c: ...     if character in ".,;!?": ...             outputCharacter = " %s" % character ...     else: ...             outputCharacter = character ...     separatedPunctuation += outputCharacter &gt;&gt;&gt; print separatedPunctuation help , me &gt;&gt;&gt; print separatedPunctuation.split() ['help', ',', 'me']   This produces the result I want, but is painfully slow on large files.  Is there a way to do this more efficiently?
54053
Why do PHP regexes have the surrounding delimiters? It seems like it would be more clear if any pattern modifiers were passed in as a parameter to whatever function was being used.
65159
I have a text file which I want to split into 64 unequal parts, according to the 64 hexagrams of the Yi Jing. Since the passage for each hexagram begins with some digit(s), a period, and two newlines, the regex should be pretty easy to write.  But how do I actually split the text file into 64 new files according to this regex? It seems like more of a task for perl. But maybe there's a more obvious way that I'm just totally missing.
90307
I have a text file as follows:  test1,test2,test3 test4,test5 test6,test7,test8,test9,test10 test11,test12,test13,test14   How can I replace commas with semicolons, starting with the second one (and continuing to the last one)?  I want to get output as follows:  test1,test2;test3 test4,test5 test6,test7;test8;test9;test10 test11,test12;test13;test14
99957
I'm trying to parse a simple string in C++.  I know the string contains some text with a colon, followed immediately by a space, then a number.  I'd like to extract just the number part of the string.  I can't just tokenize on the space (using sstream and &lt;&lt;) because the text in front of the colon may or may not have spaces in it. Some example strings might be:  Total disk space: 9852465 Free disk space: 6243863 Sectors: 4095  I'd like to use the standard library, but if you have another solution you can post that too, since others with the same question might like to see different solutions.
57185
I want to split a character string n by n. I know how to do that 1 by 1 :  \documentclass{article} \makeatletter  \def\s@plit&lt;#1#2&gt;{%   \ifx\empty#2%     #1%   \else     #1,\s@plit&lt;#2&gt;%   \fi}  \def\Split#1#2{\s@plit&lt;#2\empty&gt;} \makeatother  \begin{document} \Split{1}{123456789} \end{document}   It gives : 1,2,3,4,5,6,7,8,9  But I also want 1,23,45,67,89 or 123,456,789 ... depending from the first parameter.  EDIT  Work straight and reverse, even with foreach.        \documentclass{article}     \usepackage{tikz,xparse,xstring}  \DeclareDocumentCommand{\split}{smmo}{%     \xdef\Entree{#3}%     \let\Sortie\empty%     \IfBooleanTF #1     {% sans étoile         \loop         \StrRight{\Entree}{#2}[\tmp]%         \edef\Sortie{\Sortie,\tmp}%         \StrGobbleRight{\Entree}{#2}[\Entree]%         \unless\ifx\Entree\empty         \repeat         \StrGobbleLeft{\Sortie}{1}[\Sortie]%     }     {% avec étoile dans l'ordre inverse         \loop         \StrRight{\Entree}{#2}[\tmp]%         \edef\Sortie{\tmp,\Sortie}%         \StrGobbleRight{\Entree}{#2}[\Entree]%         \unless\ifx\Entree\empty         \repeat         \StrGobbleRight{\Sortie}{1}[\Sortie]%     }     \IfNoValueTF{#4}{\Sortie}{\edef#4{\Sortie}}     }  \begin{document} \split{2}{6513782}  \split*{2}{6513782}  \split*{2}{6513782}[\bob]  \foreach \x in \bob {[\x] }  \end{document}
40669
I want to reverse a regular expression. I.e. given a regular expression, I want to produce any string that will match that regex.  I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do this. :)  I'm using Python, so I'd like a Python library.  To reiterate, I only want one string that will match the regex. Things like "." or ".*" would make an infinite amount of strings match the regex, but I don't care about all options.  I'm willing for this library to only work on a certain subset of regex.
18905
I've got a text file full of records where each field in each record is a fixed width.  My first approach would be to parse each record simply using string.Substring().  Is there a better way?   For example, the format could be described as:  &lt;Field1(8)&gt;&lt;Field2(16)&gt;&lt;Field3(12)&gt;   And an example file with two records could look like:  SomeData0000000000123456SomeMoreData Data2   0000000000555555MoreData       I just want to make sure I'm not overlooking a more elegant way than Substring().    Update:  I ultimately went with a regex like Killersponge suggested:  private readonly Regex reLot = new Regex(REGEX_LOT, RegexOptions.Compiled); const string REGEX_LOT = "^(?&lt;Field1&gt;.{6})" +                         "(?&lt;Field2&gt;.{16})" +                         "(?&lt;Field3&gt;.{12})";   I then use the following to access the fields:  Match match = reLot.Match(record); string field1 = match.Groups["Field1"].Value;
79970
I would like to use the .Net Regex.Split method to split this input string into an array. It must split on whitespace unless it is enclosed in a quote.  Input:  Here is "my string"&nbsp;&nbsp;&nbsp;&nbsp;it has "six&nbsp;&nbsp;matches"  Expected output:   Here is my string it has six&nbsp;&nbsp;matches   What pattern do I need? Also do I need to specify any RegexOptions?
37476
What is the easiest way to match non-ASCII characters in a regex? I would like to match all words individually in an input string, but the language may not be English, so I will need to match things like ü, ö, ß, and ñ. Also, this is in Javascript/jQuery, so any solution will need to apply to that.
66832
Given the below XML snippet I need to get a list of name/value pairs for each child under DataElements. XPath or an XML parser cannot be used for reasons beyond my control so I am using regex.  &lt;?xml version="1.0"?&gt; &lt;StandardDataObject xmlns="myns"&gt;   &lt;DataElements&gt;     &lt;EmpStatus&gt;2.0&lt;/EmpStatus&gt;     &lt;Expenditure&gt;95465.00&lt;/Expenditure&gt;     &lt;StaffType&gt;11.A&lt;/StaffType&gt;     &lt;Industry&gt;13&lt;/Industry&gt;   &lt;/DataElements&gt;   &lt;InteractionElements&gt;     &lt;TargetCenter&gt;92f4-MPA&lt;/TargetCenter&gt;     &lt;Trace&gt;7.19879&lt;/Trace&gt;   &lt;/InteractionElements&gt; &lt;/StandardDataObject&gt;   The output I need is:      [{EmpStatus:2.0},  {Expenditure:95465.00}, {StaffType:11.A}, {Industry:13}]    The tag names under DataElements are dynamic and so cannot be expressed literally in the regex. The tag names TargetCenter and Trace are static and could be in the regex but if there is a way to avoid hardcoding that would be preferable.   "&lt;([A-Za-z0-9]+?)&gt;([A-Za-z0-9.]*?)&lt;/"   This is the regex I have constructed and it has the problem that it erroneously includes {Trace:719879} in the results.  Relying on new-lines within the XML or any other apparent formatting is not an option.  Below is an approximation of the Java code I am using:  private static final Pattern PATTERN_1 = Pattern.compile(..REGEX..); private List&lt;DataElement&gt; listDataElements(CharSequence cs) {     List&lt;DataElement&gt; list = new ArrayList&lt;DataElement&gt;();     Matcher matcher = PATTERN_1.matcher(cs);     while (matcher.find()) {         list.add(new DataElement(matcher.group(1), matcher.group(2)));     }     return list; }   How can I change my regex to only include data elements and ignore the rest?
97702
I'm trying to split a string with all non-alphanumeric characters as delimiters yet Java's String.split() method discards the delimiter characters from the resulting array. Is there a way to split a string like the "\W" regex pattern does, yet keep the delimiters?
26213
I can't seem to find an answer to this problem, and I'm wondering if one exists. Simplified example:  Consider a string "nnnn", where I want to find all matches of "nn" - but also those that overlap with each other. So the regex would provide the following 3 matches:   nnnn nnnn nnnn   I realize this is not exactly what regexes are meant for, but walking the string and parsing this manually seems like an awful lot of code, considering that in reality the matches would have to be done using a pattern, not a literal 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).
7002
PHP's explode function returns an array of strings split on some provided substring. It will return empty strings when there are leading, trailing, or consecutive delimiters, like this: var_dump(explode('/', '1/2//3/')); array(5) {   [0]=&gt;   string(1) &quot;1&quot;   [1]=&gt;   string(1) &quot;2&quot;   [2]=&gt;   string(0) &quot;&quot;   [3]=&gt;   string(1) &quot;3&quot;   [4]=&gt;   string(0) &quot;&quot; }  Is there some different function or option or anything that would return everything except the empty strings? var_dump(different_explode('/', '1/2//3/')); array(3) {   [0]=&gt;   string(1) &quot;1&quot;   [1]=&gt;   string(1) &quot;2&quot;   [2]=&gt;   string(1) &quot;3&quot; }
51462
If I grep a document that contains the following:  ThisExampleString  ...for the expression This*String or *String, nothing is returned. However, This* returns the above line as expected.  Whether the expression is enclosed in quotes makes no difference.  I thought the asterisk indicated any number of unknown characters? Why does it only work if it's at the start of the expression? If this is intended behavior, what do I use instead of the expressions This*String and *String?
18012
I have a single string that contains the command-line parameters to be passed to another executable and I need to extract the string[] containing the individual parameters in the same way that C# would if the commands had been specified on the command-line. The string[] will be used when executing another assemblies entry-point via reflection.  Is there a standard function for this? Or is there a preferred method (regex?) for splitting the parameters correctly? It must handle '"' delimited strings that may contain spaces correctly, so I can't just split on ' '.  Example string:  string parameterString = @"/src:""C:\tmp\Some Folder\Sub Folder"" /users:""abcdefg@hijkl.com"" tasks:""SomeTask,Some Other Task"" -someParam foo";   Example result:  string[] parameterArray = new string[] {    @"/src:C:\tmp\Some Folder\Sub Folder",   @"/users:abcdefg@hijkl.com",   @"tasks:SomeTask,Some Other Task",   @"-someParam",   @"foo" };   I do not need a command-line parsing library, just a way to get the String[] that should be generated.  Update: I had to change the expected result to match what is actually generated by C# (removed the extra "'s in the split strings)