regex

Matching a pair of comments in HTML using regular expressions

I have a mako template that looks something like this: % if staff: <!-- begin staff --> ... <!-- end staff --> % endif That way if I pass the staff variable as being True, those comments should appear. I'm trying to test this by using a regular expression that looks like this: re.search('<!-- begin staff -->.*<!-- end st...

Difference in regex behavior between Perl and Python?

I have a couple email addresses, '[email protected]' and '[email protected]'. In perl, I could take the To: line of a raw email and find either of the above addresses with /\w+@(tickets\.)?company\.com/i In python, I simply wrote the above regex as '\w+@(tickets\.)?company\.com' expecting the same result. However, support...

How can I grep for files that have both expressions?

Hello All, I would like to display files that contain more than one expression and exclude files that do not have all expressions. Any ideas? Thanks! ...

Why doesn't the regex match when I add groups?

I have this regex code in python : if re.search(r"\{\\fad|fade\(\d{1,4},\d{1,4}\)\}", text): print(re.search(r"\{\\fad|fade\((\d{1,4}),(\d{1,4})\)\}", text).groups()) text is {\fad(200,200)}Épisode 101 : {\i1}The Ghost{\i0}\Nv. 1.03 and read from a file (don't know if that helps). This returns the following: (None, None) When I ...

.htaccess Optional characters?

Though .htaccess, I want to redirect /page/var1/var2 to ./page.php?var1=var1&var2=var2. This is very easy BUT I want to make it so /page/var1 also redirects to ./page.php?var1=var1 (without having the var2). The only way I'm doing this is: RewriteRule page/(.*)$ ./page.php?var1=$1 RewriteRule page/(.*)/(.*)$ ./page.php?var1=$1&var2=$2 ...

Get the number between specified strings

Ok. Given the example of: http://example.com/news/3226-some-post-title.html I'm trying to get the 3226. This regexp: http:\/\/interaktywnie.com\/newsy\/(.*).html doesn't seem to work. Please help. Thanks. ...

Pandigital Regex?

What's a regular expression that validates if a string is pandigital (containing all digits from 1 to 9 exactly once)? For example: 123456789 891364572 But not: 11234556789 25896471 I know how to do this without regex but I was unable to form a regex for it. Thanks. This is not homework. ...

php regex to remove HTML

Before we start, strip_tags() doesn't work. now, I've got some data that needs to be parsed, the problem is, I need to get rid of all the HTML that has been formated very strangely. the tags look like this: (notice the spaces) < p > blah blah blah < / p > < a href= " link.html " > blah blah blah < /a > All the regexs I've been tr...

PHP: Escape Quotes ONLY outside of HTML tags (Regex)

What regular expression can identify double quotes outside of HTML tags (which already will be validated) to escape them to &quot;? ...

Rewrite URL from http://example.com/blog/ to http://blog.example.com/

What RewriteRule (using .htaccess/mod_rewrite) should I use to redirect http://example.com/blog/ (with www or without) to http://blog.example.com/ ? I'm using the following, but getting a redirect loop: RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] RewriteRule ^(.*)$ http://www.example.com/blog/ [L,R=301] Rewrite...

awk or sed: Best way to grab [this text]

I'm trying to parse various info from log files, some of which is placed within square brackets. For example: Tue, 06 Nov 2007 10:04:11 INFO processor:receive: [someuserid], [somemessage] msgtype=[T] What's an elegant way to grab 'someuserid' from these lines, using sed, awk, or other unix utility? ...

Regular Expression: how can I impose a perfect string matching?

Currently I am using this one ( edit: I missed to explain that I use this one for excluding exactly these words :p ): String REGEXP = "^[^(REG_)?].*"; but matches (exluding) also ERG, EGR, GRE, etc... above P.S. I removed super because it is another keyword that I must filter, figure an array list composed with more of the foll...

Extract a string within "> and </ pattern

I am trying to extract the string from a file having following pattern within a line >------ </ The ----- represents can be any variable length string. The start pattern within a line is > and end pattern </. Using regex of VIM is command line search possible? and if so could that be printed? Or will have to write a script? I am a ...

iPhone/Cocoa: NSPredicate regex replace?

According to this guide, I can use NSPredicate to do regex matching on strings, i.e., the perl equivalent of $my_string =~ m/[some regex]/ But can I do regex replace, i.e. the equivalent of this perl expression: $my_string =~ s/[pattern]/[replacement]/g ? ...

Regular expression for a non-zero hex value.

Hello, I am looking for a regular expression to determine when any of the values in a 32-bit hex value is non-zero. The data patterns look like 0x00000000 and I want to know when any of the digits is non-zero. For example, if 0x00001000 or 0x10000000 or 0xB000000 would be capture by the regular expression, but not a 0x00000000 patter...

Multi-line regex with overlapping matches

I'm working on a tool that parses files for CSS style declarations. It uses a very complicated regular expression that, besides the expected performance issues and a few minor bugs that aren't affecting me for now, is doing everything I'd like it to do except for one thing. I have it matching all combinations of element names, classes,...

Python Regexp problem

I'm trying to regexp a line from a webpage. The line is as follows: <tr><td width=60 bgcolor='#ffffcc'><b>random Value</b></td><td align=center width=80> This is what I tried, but it doesn't seem to work, can anyone help me out? 'htmlbody' contains the html page and no, I did not forget to import 're'. reg = re.compile("<tr><td width...

Searching for web links in an NSString

I have an NSString with web links in it. I need to find all web links in the string starting with http://. What is the best way to do this? ...

How can I replace each URL in a string with another unique URL?

I have the following: $reg[0] = '`<a(\s[^>]*)href="([^"]*)"([^>]*)>`si'; $reg[1] = '`<a(\s[^>]*)href="([^"]*)"([^>]*)>`si'; $replace[0] = '<a$1href="http://www.yahoo.com"$3&gt;'; $replace[1] = '<a$1href="http://www.live.com"$3&gt;'; $string = 'Test <a href="http://www.google.com"&gt;Google!!&lt;/a&gt;Test <a href="http://www.google.com"...

Why are regular expressions so controversial?

On the one hand, there are many people who seem to see regular expressions as the holy grail. Something that looks so complicated just must be the answer to any question. They think that every problem is solvable using regular expressions. On the other hand, there are also many people who try to avoid regular expressions at any cost. ...