regex

Capture the contents of a regex and delete them, efficiently.

Situation: text: a string R: a regex that matches part of the string. This might be expensive to calculate. I want to both delete the R-matches from the text, and see what they actually contain. Currently, I do this like: import re ab_re = re.compile("[ab]") text="abcdedfe falijbijie bbbb laifsjelifjl" ab_re.findall(text) # ['...

How to master Regular Expressions?

I'd like to improve my regex knowledge. Are there any exercises/tools that you wolud recommend? ...

Regular Expression Uppercase Replacement in C#

I have the following C# which simply replaces parts of the input string that look like EQUIP:19d005 into URLs, like this: input = Regex.Replace(input, @"(EQUIP:)(\S+)", @"<a title=""View equipment item $2"" href=""/EquipmentDisplay.asp?eqnum=$2"">$1$2</a>", RegexOptions.IgnoreCase); The HTML ends up looking like this. <a title="View ...

Random string that matches a regexp

How would you go about creating a random alpha-numeric string that matches a certain regular expression? This is specifically for creating initial passwords that fulfill regular password requirements. ...

Using regex to replace all spaces NOT in quotes in Ruby

I'm trying to write a regex to replace all spaces that are not included in quotes so something like this: a = 4, b = 2, c = "space here" would return this: a=4,b=2,c="space here" I spent some time searching this site and I found a similar q/a ( http://stackoverflow.com/questions/79968/split-a-string-by-spaces-in-python#80449 ) that w...

PHP validation/regex for URL

I've been looking for a simple regex for URL's, does anybody have one handy that works well? I didn't find one with the zend framework validation classes and have seen several implementations. Thanks ...

How do I replace multiple spaces with a single space in C#?

How can I replace multiple spaces in a string with only one space in C#? Example "1 2 3 4 5" would be : "1 2 3 4 5"? ...

How do I match part of a string only if it is not preceded by certain characters?

I've created the following regex pattern in an attempt to match a string 6 characters in length ending in either "PRI" or "SEC", unless the string = "SIGSEC". For example, I want to match ABCPRI, XYZPRI, ABCSEC and XYZSEC, but not SIGSEC. (\w{3}PRI$|[^SIG].*SEC$) It is very close and sort of works (if I pass in "SINSEC", it returns a ...

Replacing spaces using regex in php

I'm pretty new to regular expressions. I have a requirement to replace spaces in a piece of multi-line text. The replacement rules are these: Replace all spaces at start-of-line with a non-breaking space (&nbsp;) Replace any instance of repeated spaces (more than one space together) with the same number of non-breaking-spaces Single s...

Matching a time string with a regular expression

I would like to match the time (10.00) from a string with the date and time ("21.01.08 10.00"). I'm using the following regular expression: new RegExp("\\b[0-9]{1,2}\\.[0-9]{1,2}\\b" "g"); But this matches 21.01 from 21.01.08 and 10.00. I'm using PCRE as my regualar expression engine. Update: I'm sorry, i should have more been mor...

Why am I seeing inconsistent JavaScript logic behavior looping with an alert() vs. without it?

I have code similar to this filtering entries in an Array of Objects: var filterRegex = new RegExp(".*blah.*","ig"); if (filterRegex.test(events[i].thing) && events[i].show) { console.log("SUCCESS: filtering thing " + i + " " + events[i].thing); events[i].show = false; numevents--; } I get inconsistent results with this if...

Regex for pulling data out of quotes?

I'm looking for a regex that can pull out quoted sections in a string, both single and double quotes. IE: "This is 'an example', \"of an input string\"" Matches: an example of an input string I wrote up this: [\"|'][A-Za-z0-9\\W]+[\"|'] It works but does anyone see any flaws with it? EDIT: The main issue I see is that it can...

Compile regex in PHP

Is there a way in PHP to compile a regular expression, so that it can then be compared to multiple strings without repeating the compilation process? Other major languages can do this -- Java, C#, Python, Javascript, etc. ...

Regex greedy issue

I'm sure this one is easy but I've tried a ton of variations and still cant match what I need. The thing is being too greedy and I cant get it to stop being greedy. Given the text: test=this=that=more text follows I want to just select: test= I've tried the following regex (\S+)=(\S.*) (\S+)?= [^=]{1} ... Thanks all. ...

What is a "Nested Quantifier" and why is it causing my regex to fail?

I have this regex I built and tested in regex buddy. "_ [ 0-9]{10}+ {1}+[ 0-9]{10}+ {2}+[ 0-9]{6}+ {2}[ 0-9]{2}" When I use this in .Net C# I receive the exception "parsing \"_ [ 0-9]{10}+ +[ 0-9]{10}+ +[ 0-9]{6}+ [ 0-9]{2}\" - Nested quantifier +." What does this error mean? Apparently .net doesn't like the expression. Here is ...

Regex multi word search

What do I use to search for multiple words in a string? I would like the logical operation to be AND so that all the words are in the string somewhere. I have a bunch of nonsense paragraphs and one plain English paragraph, and I'd like to narrow it down by specifying a couple common words like, "the" and "and", but would like it match ...

What would be a globally accepted regular expression to match e-mail addresses

I have seen many examples, with many 'no, you missed something' comments. What is the right way to match an e-mail address? For Sanity sake, only fully-qualified domain names, no @localhost allowed. (or, both ways) Subdomains must be allowed ([email protected]) ...

How can I extract and save text using Perl?

No extracted data output to data2.txt? What goes wrong to the code? MyFile.txt ex1,fx2,xx1 mm1,nn2,gg3 EX1,hh2,ff7 This is my desired output in data2.txt: ex1,fx2,xx1 EX1,hh2,ff7 #! /DATA/PLUG/pvelasco/Softwares/PERLINUX/bin/perl -w my $infile ='My1.txt'; my $outfile ='data2.txt'; open IN, '<', $infile or die "Cant open $in...

Why are people using regexp for email and other complex validation?

There are a number of email regexp questions popping up here, and I'm honestly baffled why people are using these insanely obtuse matching expressions rather than a very simple parser that splits the email up into the name and domain tokens, and then validates those against the valid characters allowed for name (there's no further check ...

Regex for repeated words with punctuation

What I want to do is check for duplicated words right next to each other but even if there is punctuation in between. For example: Vivamus Vivamus diam, diam, Vivamus Vivamus diam, diam Vivamus there should be 4 distinct hits here. I can't figure out why this isn't working; can someone explain why and show me what the correct code s...