regex

Does Perl's /m regex modifier match differently on Windows?

The following Perl statements behave identically on Unixish machines. Do they behave differently on Windows? If yes, is it because of the magic \n? split m/\015\012/ms, $http_msg; split m/\015\012/s, $http_msg; I got a failure on one of my CPAN modules from a Win32 smoke tester. It looks like it's an \r\n vs \n issue. One chan...

Regex for strings ending in .cs but not in .g.cs

I need a regex that matches all strings ending in .cs, but if they end in .g.cs they should not match. I'm using .NET regular expressions. ...

Avoiding SQL injection in a user-generated SQL-regex

Hi. I'm creating a site where the user unfortunately has to provide a regex to be used in a MySQL WHERE clause. And of course I have to validate the user input to prevent SQL injection. The site is made in PHP, and I use the following regex to check my regex: /^([^\\\\\']|\\\.)*$/ This is double-escaped because of PHP's way of handling...

Trim whitespace from middle of string

I'm using the following regex to capture a fixed width "description" field that is always 50 characters long: (?.{50}) My problem is that the descriptions sometimes contain a lot of whitespace, e.g. "FLUID COMPRESSOR " Can somebody provide a regex that: Trims all whitespace off the end Collapses an...

bash regex with quotes?

The following code number=1 if [[ $number =~ [0-9] ]] then echo matched fi works. If I try to use quotes in the regex, however, it stops: number=1 if [[ $number =~ "[0-9]" ]] then echo matched fi I tried "\[0-9\]", too. What am I missing? Funnily enough, bash advanced scripting guide suggests this should work. Bash version 3....

How do I stop UrlRewriter stripping my + symbols

I'm using Intelligencia's UrlRewriter in my application and I'm having a problem where the rules I've setup appear to be stripping the + symbol from my url. For example I want to have the urls /category/catname/+tag+tag but it appears to me as /category/catname/ tag tag Does anyone have any ideas and is this down to my regular expressi...

Can I test if a regex is valid in C# without throwing exception

I allow users to enter a regular expression to match IP addresses, for doing an IP filtration in a related system. I would like to validate if the entered regular expressions are valid as a lot of userse will mess op, with good intentions though. I can of course do a Regex.IsMatch() inside a try/catch and see if it blows up that way, bu...

Why ^*$ matches "127.0.0.1"

I don't understand, why does the following regular expression: ^*$ Match the string "127.0.0.1"? Using Regex.IsMatch("127.0.0.1", "^*$"); Using Expresso, it does not match, which is also what I would expect. Using the expression ^.*$ does match the string, which I would also expect. Technically, ^*$ should match the beginning of a s...

What is the meaning of '(?i)password' in python regular expression?

Pexpect can be used to automate tasks in python (does not need TCL to be installed). One of the simplest routines of this class is the 'run()' routine. It accepts a dictionary of expected question patterns as keys and the responses as values. For example pexpect.run ('scp foo [email protected]:.', events={'(?i)password': mypasswor...

Adding an AND clause to a regex.

I have this simple regex, [\d]{1,5} that matches any integer between 0 and 99999. How would I modify it so that it didn't match 0, but matches 01 and 10, etc? I know there is a way to do an OR like so... [\d]{1,5}|[^0]{1} (doesn't make much sense) There a way to do an AND? ...

Parse filename from full path using regular expressions in C#

How do I pull out the filename from a full path using regular expressions in C#? Say I have the full path C:\CoolDirectory\CoolSubdirectory\CoolFile.txt. How do I get out CoolFile.txt using the .NET flavor of regular expressions? I'm not really good with regular expressions, and my RegEx buddy and me couldn't figure this one out. Also...

Supposedly valid regular expression doesn't return any data in PHP

I am using the following code: <?php $stock = $_GET[s]; //returns stock ticker symbol eg GOOG or YHOO $first = $stock[0]; $url = "http://biz.yahoo.com/research/earncal/".$first."/".$stock.".html"; $data = file_get_contents($url); $r_header = '/Prev. Week(.+?)Next Week/'; $r_date = '/\<b\>(.+?)\<\/b\>/'; preg_match($r_header,$data,$he...

Converting a sed string to PHP

Hi, I have a regexp I'm using with sed, but now I need to make it work in PHP also. I can't use system calls as they are disabled. $ cat uglynumber.txt: Ticket number : 303905694, FOO:BAR:BAR: Some text Case ID:123,456,789:Foobar - Some other text 303867970;[FOOBAR] Some text goes here Case Ref: 303658850 - Some random text here - hos...

does regex comparisons consume lots of resources?

i dunno, but will your machine suffer great slowdown if you use a very complex regex? like for example the famous email validation module proposed just recently? which can be found here RFC822 update: sorry i had to ask this question in a hurry anyway i posted the link to the email regex i was talking about ...

Question about URL Validation with Regex

I have the following regex that does a great job matching urls: ((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)` However, it does not handle urls without a prefix, ie. stackoverflow.com or www.google.com do not match. Anyone know how I can modify this regex to not care if there is a prefix ...

How can I remove an entire HTML tag (and its contents) by its class using a regex?

Hi, I am not very good with Regex but I am learning. I would like to remove some html tag by the class name. This is what I have so far : <div class="footer".*?>(.*?)</div> The first .*? is because it might contain other attribute and the second is it might contain other html stuff. What am I doing wrong? I have try a lot of set wit...

Make a Perl-style regex interpreter behave like a basic or extended regex interpreter

I am writing a tool to help students learn regular expressions. I will probably be writing it in Java. The idea is this: the student types in a regular expression and the tool shows which parts of a text will get matched by the regex. Simple enough. But I want to support several different regex "flavors" such as: Basic regular expres...

Javascript percentage validation

I am after a regular expression that validates a percentage from 0 100 and allows two decimal places. Does anyone know how to do this or know of good web site that has example of common regular expressions used for client side validation in javascript? @Tom - Thanks for the questions. Ideally there would be no leading 0's or other tra...

RegEx teaser

Let's say we have 2 php variables: $name = 'caption'; $url = 'http://domain.com/photo.jpg'; The input string of '{@url,<img src="," alt="{@name}" />}' should return: '<img src="http://domain.com/photo.jpg" alt="caption" />' The {tag} takes up to 3 parameters: {@variable[,text_before][,text_after]}. What regex would be needed to ma...

Can you propose a more elegant way to 'tokenize' c# code for html formatting?

(This question about refactoring F# code got me one down vote, but also some interesting and useful answers. And 62 F# questions out of the 32,000+ on SO seems pitiful, so I'm going to take the risk of more disapproval!) I was trying to post a bit of code on a blogger blog yesterday, and turned to this site, which I had found useful in ...