regex

How can I find the first occurrence of a pattern in a string from some starting position?

I have a string of arbitrary length, and starting at position p0, I need to find the first occurrence of one of three 3-letter patterns. Assume the string contain only letters. I need to find the count of triplets starting at position p0 and jumping forward in triplets until the first occurrence of either 'aaa' or 'bbb' or 'ccc'. Is th...

Javascript Try/Catch

I've got a function that runs a user generated Regex. However, if the user enters a regex that won't run then it stops and falls over. I've tried wrapping the line in a Try/Catch block but alas nothing happens. If it helps, I'm running jQuery but the code below does not have it as I'm guessing that it's a little more fundamental than th...

Regular expression to remove XML tags and their content

I have the following string and I would like to remove <bpt *>*</bpt> and <ept *>*</ept> (notice the additional tag content inside them that also needs to be removed) without using a XML parser (overhead too large for tiny strings). The big <bpt i="1" x="1" type="bold"><b></bpt>black<ept i="1"></b></ept> <bpt i="2" x="2" type="ulined">...

How can I add a REGEX match to my J2ME project?

The question pretty much sums it up. Just want to run a regular expression match on a string in J2ME. ...

How do you translate this regular-expression idiom from Perl into Python?

I switched from Perl to Python about a year ago and haven't looked back. There is only one idiom that I've ever found I can do more easily in Perl than in Python: if ($var =~ /foo(.+)/) { # do something with $1 } elsif ($var =~ /bar(.+)/) { # do something with $1 } elsif ($var =~ /baz(.+)/) { # do something with $1 } The corres...

Is there an Application to Create Regular Expression Out of Text by Selecting Wanted Area?

Hey, I hope this is programmer-related question. I'm in the hobby business of C# programming. For my own purposes I need to parse html files and the best idea is..regular expression. As many found out, it's quite time consuming to learn them and thus I'm quite interested if you know about some application that would be able to take inpu...

Regex - Repeating Header in a group

I am parsing text that has a heading and then data that applies to that heading. I need to break each data field into groups, and have the heading also apply to those groups. Here's an example: (Update: The text below has been updated to better reflect it's current layout, and to indicate an annotation.) Heading 1 Heading 2 H...

Invalid group name: Group names must begin with a word character

I received the following exception when I was using the Regex class with the regular expression: (?'named a'asdf) System.ArgumentException: parsing \"(?'named a'asdf)\" - Invalid group name: Group names must begin with a word character. What is the problem with my regular expression? ...

A comprehensive regex for phone number validation

I'm trying to put together a comprehensive regex to validate phone numbers. Ideally it would handle international formats, but it must handle US formats, including the following: 1-234-567-8901 1-234-567-8901 x1234 1-234-567-8901 ext1234 1 (234) 567-8901 1.234.567.8901 1/234/567/8901 12345678901 I'll answer with my current attempt, b...

Shell script to recursively browse a directory and replace a string

I need to recursively search directories and replace a string (say http://development:port/URI) with another (say http://production:port/URI) in all the files where ever it's found. Can anyone help? It would be much better if that script can print out the files that it modified and takes the search/replace patterns as input parameters. ...

Passing a regex substitution as a variable in Perl?

I need to pass a regex substitution as a variable: sub proc { my $pattern = shift; my $txt = "foo baz"; $txt =~ $pattern; } my $pattern = 's/foo/bar/'; proc($pattern); This, of course, doesn't work. I tried eval'ing the substitution: eval("$txt =~ $pattern;"); but that didn't work either. What horribly obvious thing a...

XML vs Text for Non-web development applications

I do alot of systems programming where my apps have no chance of being used to communicate over the web or viewed through a browser. But, there has been some push by management to use XML. For example, if I want to keep a time log I could use a text file like this: command date time project in 2008/09/23 08:00:00 PROJ1 change 2008/09/...

Find out number of capture groups in Python regular expressions

Is there a way to determine how many capture groups there are in a given regular expression? I would like to be able to do the follwing: def groups(regexp, s): """ Returns the first result of re.findall, or an empty default >>> groups(r'(\d)(\d)(\d)', '123') ('1', '2', '3') >>> groups(r'(\d)(\d)(\d)', 'abc') ('', '...

PHP: using preg_replace with htmlentities

I'm writing an RSS to JSON parser and as a part of that, I need to use htmlentities() on any tag found inside the description tag. Currently, I'm trying to use preg_replace(), but I'm struggling a little with it. My current (non-working) code looks like: $pattern[0] = "/\<description\>(.*?)\<\/description\>/is"; $replace[0] = '<descript...

JavaScript or Java String Subtraction

If you are using Java or JavaScript, is there a good way to do something like a String subtraction so that given two strings: org.company.project.component org.company.project.component.sub_component you just get sub_component I know that I could just write code to walk the string comparing characters, but I was hoping there was a ...

What's the (JavaScript) Regular Expression I should use to ensure a string is a valid file name?

I'm still learning RegEx at the moment, but for the time being could someone help me out with this? I have a few special requirements for formatting the string: No directories. JUST the file name. File name needs to be all lowercase. Whitespaces need to be replaced with underscores. Shouldn't be hard, but I'm pressed for time and I...

How to (with SimpleTest) write an AssertTags test with regex?

I wish to test a function that will generate lorem ipsum text, but it does so within html tags. So I cant know in advance the textual content, but i know the html structure. That is what I want to test. And maybe that the length of the texts are within certain limits. So what I am wondering is if the assertTags can do this in a way parap...

Invert Regex Matches

How would I invert .NET regex matches? I want to extract only the matched text, e.g. I want to extract all IMG tags from an HTML file, but only the image tags. ...

Free alternative to RegxBuddy

Are there any good alternatives that support writing regexps in different flavours and allow to test them? ...

Regex to remove conditional comments

I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments. I would also like to avoid using the .*? notation if possible. The text is foo <!--[if IE]> <style type="text/css"> ul.menu ul li{ font-size: 10px; font-weight:normal; padding-...