I have 2 strings and I want to find all locations of one string within another. String.find works but not regex.  sub: (dont want to) call in   str1: i cant (dont want to) call in   str1.find(sub) returns 7 but     [(m.start(0), m.end(0)) for m in re.finditer(sub,str1)] returns empty list
591
I am tired of always trying to guess, if I should escape special characters like '()[]{}|' etc. when using many implementations of regexps.  It is different with, for example, Python, sed, grep, awk, Perl, rename, Apache, find and so on. Is there any rule set which tells when I should, and when I should not, escape special characters? Does it depend on the regexp type, like PCRE, POSIX or extended regexps?
1424
I have the string   a.b.c.d   I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner.  (Previously I had expressed this constraint as "without a loop", in case you're wondering why everyone's trying to answer without using a loop).
3235
I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence:  grep pattern1 | grep pattern2 | ...   So how to convert it to something like?  grep pattern1 &amp; pattern2 &amp; pattern3   I would like to use single grep because I am building arguments dynamically, so everything has to fit in one string. Using filter is system feature, not grep, so it is not an argument for it.    Don't confuse this question with:  grep "pattern1\|pattern2\|..."   This is an OR multi pattern match.
56815
Is there a way to specify a regular expression to match every 2nd occurrence of a pattern in a string?  Examples   searching for a against string abcdabcd should find one occurrence at position 5 searching for ab  against string abcdabcd should find one occurrence at position 5 searching for dab  against string abcdabcd should find no occurrences searching for a against string aaaa should find two occurrences at positions 2 and 4
8223
I have been trying to create a find command string that will find all files that end with a number 1-99 but exclude all others.  e.g. I want to find myfile1 myfile99 but not myfile456 and not myfilebackup  The regex I'm thinking of is myfile[1-9]{1,2} but I can't get this to work with find.  find . -regex '.*myfile[0-9]{1,2}' OR  find . -iname 'myfile[0-9]{1,2}'  From what I can see it's the {1,2} part that is not working.  (by the way can you use -name -regex interchangably?)  Any help appreciated.
112773
I need to be able to take a string like:  '''foo, bar, "one, two", three four'''   into:  ['foo', 'bar', 'one, two', 'three four']   I have an feeling (with hints from #python) that the solution is going to involve the shlex module.
98081
What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'?  The regular expression should match any of the following:  RunFoo.py RunBar.py Run42.py   It should not match:  myRunFoo.py RunBar.py1 Run42.txt   The SQL equivalent of what I am looking for is ... LIKE 'Run%.py' ....
4355
For example, this regex  (.*)&lt;FooBar&gt;   will match:  abcde&lt;FooBar&gt;   But how do I get it to match across multiple lines?  abcde fghij&lt;FooBar&gt;
109876
In , the votes clearly show that the os.path.splitext function is preferred over the simple .split('.')[-1] string manipulation. Does anyone have a moment to explain exactly why that is? Is it faster, or more accurate, or what? I'm willing to accept that there's something better about it, but I can't immediately see what it might be. Might importing a whole module to do this be overkill, at least in simple cases?  EDIT: The OS specificity is a big win that's not immediately obvious; but even I should've seen the "what if there isn't a dot" case! And thanks to everybody for the general comments on library usage.
23996
I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:  &gt;&gt;&gt; s = 'spam' &gt;&gt;&gt; s[:-1] 'spa'   Why does slicing with no beginning bound and a '-1' return every element except the last one? Is calling s[0:-1] logically the same as calling s[:-1]? They both return the same result. But I'm not sure what python is doing exactly. Any help would be greatly appreciated.
92395
I want to split a command line like string in single string parameters. How look the regular expression for it. The problem are that the parameters can be quoted. For example like:  "param 1" param2 "param 3"  should result in:  param 1, param2, param 3
118546
Is there a regular expression that matches valid regular expressions?   (I know there are several flavors of regexps. One would do.)
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.
4412
The Python docs say:     re.MULTILINE: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline)... By default, '^' matches only at the beginning of the string...   So what's going on when I get the following unexpected result?  &gt;&gt;&gt; import re &gt;&gt;&gt; s = """// The quick brown fox. ... // Jumped over the lazy dog.""" &gt;&gt;&gt; re.sub('^//', '', s, re.MULTILINE) ' The quick brown fox.\n// Jumped over the lazy dog.'
119008
Given this string:  SET @text = "&lt;a href=""https://salesforce.stackexchange.com""target=""_blank""&gt;Salesforce&lt;/a&gt; &lt;a href=""https://stackoverflow.com"" target=""_blank""&gt;Everything else&lt;/a&gt;"   And this function:  SET @matching = '\s+(?:[^&gt;]*?\s+)?href="([^"]*)"' SET @output = RegExMatch(@text, @matching, 1)   How can I manage to find and print an arbitrary number of matches in my string? E.g. in above example, I would like to present e.g. a rowset with each URL in each row.
48268
I have some simple python code that searches files for a string e.g. path=c:\path, where the c:\path part may vary. The current code is:  def find_path(i_file):     lines = open(i_file).readlines()     for line in lines:         if line.startswith("Path="):             return # what to do here in order to get line content after "Path=" ?   What is a simple way to get the text after Path=?
55644
I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)  I realize that I could .match() substrings with a Regex, but that doesn't solve nesting /*, or having a // inside a /* */.  Ideally, I would prefer a non-naive implementation that properly handles awkward cases.