regex

Take first successful match from a batch of regexes

I'm trying to extract set of data from a string that can match one of three patterns. I have a list of compiled regexes. I want to run through them (in order) and go with the first match. regexes = [ compiled_regex_1, compiled_regex_2, compiled_regex_3, ] m = None for reg in regexes: m = reg.match(name) if m: break ...

A regular expression question

Hi, I have content something like <div class="c2"> <div class="c3"> <p>...</p> </div> </div> What I want is to match the div.c2's inner HTML. The contents of it may vary a lot. The only problem I am facing here is that how can I make it to work so that the right closing div is taken? ...

Replace non-numeric characters

I need to replace non-numeric chars from a string. For example, "8-4545-225-144" needs to be "84545225144"; "$334fdf890==-" must be "334890". How can I do this? ...

What's the best regex for validating a field?

I use a regular expression to on a user-inputted field to make sure that they have entered between 1 and 20 characters. Here's the code: $post_validations = array("title" => '/^[[:alnum:][:punct:][:space:]]{1,100}$/'); But whenever a user enters a foreign character, or a special quote character from MS Word (I can't paste it into he...

Get Groups From Words

Hi, I'm struggling to write a reg-ex that can do the following: Label1.Caption := Edit1.Text; Must return 2x results e.g.: Label1.Caption Edit1.Text So its everything where its a word (Like "Label1"), a dot (.) and a given set of words like "Caption", "Text" etc. ...

Regex Expressions in C programming ?

Hello all, how can i use Regex Expressions in C programming? for example if i want to find a line in a file DAEMONS=(sysklogd network sshd !netfs !crond) then print each daemon in separate line like this sysklogd network sshd !netfs !crond here what i did so far #include <stdio.h> #include <stdlib.h> #include <string.h> #...

Regex to match sentences with at least n words

I'm trying to pull all sentences from a text that consist of, say, at least 5 words in PHP. Assuming sentences end with full stop, question or exclamation mark, I came up with this: /[\w]{5,*}[\.|\?|\!]/ Any ideas, what's wrong? Also, what needs to be done for this to work with UTF-8? ...

string transformation with regex

I have the following string: val s1:String = "1. my line 1\n2. my other line\n3. my 3rd line\n4. and so on" Now, I want transform at other: val s2:String = "<b>1. </b>my line 1\n<b>2. </b>my other line\n<b>3. </b>my 3rd line\n<b>4. </b>and so on" What is better way to do it? ...

PHP Regex string matching

Hi, I'm new to PHP and generally regular expressions. I have to match words that contains strings like word[0-9]* = any string here . How do I write a regex for this. So far I have come up with this but it doesnt seem to find the strings properly. $regexp = "word[0-9]* = [A-Z](.*)[a-z]"; How would I correct the above expression? Tha...

Regex negation - word parsing

I am trying to parse a phrase and exclude common words. For instance in the phrase "as the world turns", I want to exclude the common words "as" and "the" and return only "world" and "turns". (\w+(?!the|as)) Doesn't work. Feedback appreciated. ...

.NET Regular Expression Expert Help Needed

I need two regular expressions to identify if .. then .. else .. endif section and their parts. From an expression which could be like below: Example 1: 5 + 10 * (if (4 + 4.5) > 0 then 20 else 45 endif) + 2 Example 2: if (20 == 10) then 10 endif Example 3: if (20/10 != 2) then (2 * 10) else (3 * 4) endif Expected Result: A r...

javascript regex - remove a querystring variable if present

I need to rewrite a querysting using javascript. First I check to see if the variable is present, if it is I want to replace it with a new search term. If it is not present then I just add the new variable I'm able to get it to work with simple terms like hat, ufc hat whitespaces are %20, so ufc hat is really ufc%20hat I run into probl...

How can we match a^n b^n with Java regex?

This is the second part of a series of educational regex articles. It shows how lookaheads and nested references can be used to match the non-regular languge anbn. Nested references are first introduced in: How does this regex find triangular numbers? One of the archetypal non-regular languages is: L = { anbn: n > 0 } This i...

Determine whether a string is "empty"

I need a JavaScript function to tell me whether a string object is empty. By "empty", I mean that it's not all just whitespace characters. I've written this prototype: String.prototype.isEmpty = function() { return this.length === 0 || this === " " || this.test(/^\s*$/); } Is this alright? Is there a more-performant version of this...

Code coverage tools for regexes?

Are there tools out there to measure code coverage of regexes? A tool that given a regex and a list of input strings, tells you which parts of the regex are exercised, with measures analogous to statement coverage, branch coverage, condition coverage, etc. I don't care much what language or environment it runs in. (Update: after some m...

Javascript - Split dynamic string getting values inside brackets

I have this as data input (it's dynamic so it can be 1 up to 5 brackets in the same string) data["optionBase"] = {} //declaration data["optionBase"]["option"] = {} //declaration data["optionBase"]["option"][value] = {} //declaration data["optionBase"]["option"][value]["detail"] = somethingHere Each line comes as a string, not as an ar...

Regular Expression - How to find the <%@ %> line in the file?

I found there is a bug in this highlight editor: http://cshe.ds4a.com/ The following ASP.Net code can't be highlighted correctly <%@ Page Title="<%$ Resources: XXX %>" Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %> The problem is about the regular expression, how can I find this whole line by regular expression? ...

python: padding punctuation with white spaces (keeping punctuation)

What is an efficient way to pad punctuation with whitespace? input: s = 'bla. bla? bla.bla! bla...' desired output: s = 'bla . bla ? bla . bla ! bla . . .' Comments: I don't care how many whitespaces are there between tokens. (but they'll need to be collapsed eventually) I don't want to pad all punctuation. Say I'm interested o...

Batch string replace

I have a variable like this "Folder With Spaces/filename.ext" When I pass it to my program via PHP's system command, arguments are separated by spaces system("batch.bat Folder With Spaces/filename.ext"); So I have it like this system("batch.bat Folder_With_Spaces/filename.ext"); Is there a way now that it is back in the batch pr...

HTML code strip regexp problem

In javascript, one of the popular regex is to strip out HTML tags from the text. The code for that is String.prototype.stripHTML = function () { var reTag = /<(?:.|\s)*?>/g; return this.replace(reTag, ""); }; If you try this on "<b>This would be bold</b>".stripHTML(), then it outputs as "This would ...