regex

How can I match multiple occurrences with a regex in JavaScript similar to PHP's preg_match_all()?

I am trying to parse url-encoded strings that are made up of key=value pairs separated by either & or &. The following will only match the first occurrence, breaking apart the keys and values into separate result elements: var result = mystring.match(/(?:&|&)?([^=]+)=([^&]+)/) The results for the string '1111342=Adam%20Franc...

Need a regex to match a variable length string of numbers that can't be all zeros

I need to validate an input on a form. I'm expecting the input to be a number between 1 to 19 digits. The input can also start with zeros. However, I want to validate that they are not all zeros. I've got a regex that will ensure that the input is numeric and between 1 and 19 numbers. ^\d[1,19]$ But I can't figure out how to include a...

Remove brackets with regular expression in C#

I want to remove square brackets from an SQL string, but only where there is no whitespace inside them. E.g. "SELECT [intId], [The Description]" should return "SELECT intId, [The Description]". I can get the square brackets without spaces inside with the regular expression: \[[^\s]*\] How can the square brackets from these matches b...

Regex.Match() question

I am using this method to try find a match, in an example: Regex.Match("A2-TS-OIL", "TS-OIL", RegexOptions.IgnoreCase).Success; I got a true result. I am confused. I think this should return false since there is no special characters in the pattern. If I use ".+TS-OIL", true should be returned (. for any and + for more than 1). How sh...

How do I extract the text before and after a known marker with a Perl regex?

Can anybody tell me how to identify the middle part interestedInThis and backreference the prefix: fontsize=12 and postfix: fontstyle=bold as ${1} and ${2}? I'm dealing with this string: <fontsize=12 interestedInThis fontstyle=bold> Addendum: Sorry, I was not precise enough, here are the specifics: prefix and postfix could be abs...

C# regular expressions - matching whole words?

I am writing a program that needs to search a LARGE text document for a large collection of words. The words are all file names, with underscores in them (eg, this_file_name). I know how to open and iterate through a text document, but I'm curious whether I should use Regex to search for these names, and if so, what kind of reg. ex. se...

Regex http/www to links

How would a regex look that makes text that starts with either http or www clickable? My current bbcode: function bbcode($text) { $text = htmlspecialchars($text); $text = nl2br($text); $find = array( "'\[b\](.*?)\[/b\]'is", "'\[i\](.*?)\[/i\]'i", "'\[url\](.*?)\[/url\]'i" ); $r...

Can I define a Perl-like binding operator (=~) in C#?

I really like being able to use =~ and !~ in Perl to evaluate a string against a regular expression. I'd like to port this functionality over to C#, but it appears that, while you can overload operators, you can't create new ones. I'm considering extending the string type to provide a Match() method that will allow me to pass a regular ...

Regular expression to detect semi-colon terminated C++ for & while loops

In my Python application, I need to write a regular expression that matches a C++ for or while loop that has been terminated with a semi-colon (;). For example, it should match this: for (int i = 0; i < 10; i++); ... but not this: for (int i = 0; i < 10; i++) This looks trivial at first glance, until you realise that the text betwe...

Ruby Regex Help

Hi im learning Ruby Regex, but having once problem here. I want to extract links from google.com Code i shall extract from looks like this <a href="http://www.test.com/" class="l" I took me around 5min to found a regex that shall works. (with www.rubular.com) It is "(.*?)" class="l" Heres Comes the code require "open-uri" u...

Short example of regular expression converted to a state machine?

In the Stack Overflow podcast #36 (http://blog.stackoverflow.com/2009/01/podcast-36/), this opinion was expressed: Once you understand how easy it is to set up a state machine, you’ll never try to use a regular expression inappropriately ever again. I've done a bunch of searching. I've found some academic papers and other complicated e...

Regular expression: match start or whitespace

Can a regular expression match whitespace or the start of a string? I'm trying to replace currency the abbreviation GBP with a £ symbol. I could just match anything starting GBP, but I'd like to be a bit more conservative, and look for certain delimiters around it. >>> import re >>> text = u'GBP 5 Off when you spend GBP75.00' >>> re.s...

RegEx: matching and replacing ABC in "AB ABC D"

I need advice on this snippet $text = preg_replace('|(A.*)?A(.*)C|', '$1foo$2bar', $text); This will match ABC in "AB ABC D", and replace it with "AB fooBbar D"; as you can see this matches the "AB " part at the beginning as well, which I have to repeat in the replacement string with $1, in order not to lose it. Is this the best way ...

replace url paths using Regex

How can I change the url of my images from this: http://www.myOLDwebsite.com/******.*** (i have gifs, jpgs, pngs) to this: http://www.myNEWwebiste.com/somedirectory/******.*** Using REGexp text editor? Really thanks for your time []'s Mateus ...

Question about Regular Expression Books

Hi Guys, I want to purchase a regular expressions book centered around PHP. I found this one that looks ok: Friedl - Mastering Regular Expressions but it looks like general information about regular expressions and not PHP. My question is: Does the syntax for regular expressions translate exactly from one language to another, ie-> PHP...

Please help writing regular expression to extract domain name from string

Hello, I'm trying to extract the domain name from a string in C#. You don't necessarily have to use a RegEx but we should be able to extract yourdomain.com from all of the following: yourdomain.com www.yourdomain.com http://www.yourdomain.com http://www.yourdomain.com/ store.yourdomain.com http://store.yourdomain.com whatever.youdomain....

How to ignore escape sequences stored in PowerShell string variable?

In my PowerShell script, I'm running Select-String over a number of files, looking for a string passed into it via a variable ($id): foreach ($file in (ls "path\to\files")) { $found = $false $found = Select-String -Path $file $id -Quiet if ($found) { break } } Unfortunately, the $id variable sometimes things li...

Double interpolation of regular expressions in Perl

I have a Perl program that stores regular expressions in configuration files. They are in the form: regex = ^/d+$ Elsewhere, the regex gets parsed from the file and stored in a variable - $regex. I then use the variable when checking the regex, e.g. $lValid = ($valuetocheck =~ /$regex/); I want to be able to include perl variables...

Removing nonnumeric and nonalpha characters from a string?

What is the best way to remove all the special characters from a string - like these: !@#$%^&*(){}|:"?><,./;'[]\=- The items having these characters removed would rather short, so would it be better to use REGEX on each or just use string manipulation? Thx Environment == C#/.NET ...

Regex to match 2-10, but not 99

I have a field on a form that takes the following values: -1, 2-10, 99 I have a business rule that's concerned with answers 2-10. I'm trying to write a regular expression that will match 2-10 but not 99, and I'm having trouble. The original expression: ^2|3|4|5|6|7|8|9|10$ Fails because 99 is matched (technically, twice). Also, th...