regex

Any way to improve this regular expression?

Hi guys - I'm kinda a newbie at regular expressions, so would appreciate a bit of peer feedback on this one. It will be heavily used on my site, so any weird edge cases can totally wreak havoc. The idea is to type in an amount of an ingredient in a recipe in whole units or fractions. Due to my autocomplete mechanism, just a number is ...

Regular expression library benchmarks

I have been wondering about the performance of regular expression implementations lately, and have had a hard time coming up with much useful information. Its easy enough to benchmark browser/javascript regex performance (plenty of tools on the net). The javascript regex implementation in Chrome and Opera pretty much destroy every othe...

PHP regex to obtain img class

Hi! I have a bunch of text and HTML and what I want to achieve is gather all css classes of img tags that match a certain pattern and place them in a loop. For example: <img src="sample1.gif' class="get-this-tag-1" />This is text. This is text. This is text. This is text. This is text. This is text. <img src="sample2.gif' class="image...

Pattern Matching - String Search

hi guys, I am trying to work out a formula to match a following pattern: input string example: '444'/'443'/'434'/'433'/'344'/'334'/'333' if any of the patterns above exist in a particular input string I want to match it as the same pattern. also is it possible to do a variable substitution using regex? meaning check for the 3 chars ...

Java String Split Problem

I have a string such as 397 Walker Road Wayne, PA 19087 I need to extract the address information (street address, city, state, zip) from it. The problem is, the amount of white space is not consistent in the database. I've split the string on the comma, and extracted the state and zip. But am at a...

Regular expression, find word in string, but not surrounded by tag

These code find first occurance $word in $text, and replace it by something: <?php $text = preg_replace("/\b($word)\b/i", 'something', $text, 1); ?> But i want ignore if this word surrounded by "a" tag, for example, searching should find only second "word" here: <a href="something">text text word text</a>. text2 text2 word text2.....

Regular expresion to identify a function whose general type is <FUNCTION NAME>#(arg1,[arg2],[agr3]....)

Consider the below Case 1: SDN#(X,) Case 2: SDN#(X,12) Case 3: SDN#(34,12) Case 4: CORR#(X,,) Case 5: CORR#(X,12,45) Case 6: CFH#(X,AVG) These all are some kind of custom functions. How can I build a regular expression that will identity that the given text satisfies the function requirement The generalised structure is as <F...

Javascript Regex problem

I have <li> elements in my document such as: <li id="123" onMouseOver="foo();">Lorem Ipsum ... I would like to remove the attributes and get the string <li>Lorem Ipsum .... In order to do that, I use the replace() method: foo = document.getElementById('bar'); alert(foo.innerHTML.replace('<li(.*)', '<li>')); But it won't match and r...

What's the easiest way to remove all attributes from a XML in C#?

Hi, I want to remove the attributes of all tags from a XML (I want to keep only the tags and their inner value). What's the easiest way to do this in C#? ...

validate URL in form http://www.test.com

Hi, I want to validate URL (using java script) to accept only format - http://www.test.com. I'm trying following code, but it accepts test.com and http://www.test.com.com also. var URLReg = new RegExp(); URLReg.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); if (!URLReg.test(form["URL"].value)) { alert("You must su...

Regex which controls phone numbers

Hello, i'm new to regex, and i only need one statement. I want that my statement accepts these numbertyps: 06643823423 could be longer or 0664 3843455 or +43664 4356999 and it's important that these is only one statement. can anyone help me? thanks mike ...

What's wrong with my Javascript regex ?

I would like regex to match if and only if the string is none, family or work (but not my family, for example). Why the following code results with "match" ? Why ^ and $ do not work ? var regex = new RegExp("^none|family|work$"); var str = "my family"; document.write(regex.test(str) ? "match" : "no"); ...

Running a regular expression - need to find exact location of a string patterns occurrence

This is a follow up from a previous question. The regular expression is working fantastic however I need to be able to find out the exact location of the pattern occurrence. Like for example if my regular expression were to extract instances of the substring 'bob' from the following string: This is abobsled I want to also know the ex...

Splitting string into array of words using Regular Expressions

I'm trying to split a string into an array of words, however I want to keep the spaces after each word. Here's what I'm trying: var re = /[a-z]+[$\s+]/gi; var test = "test one two three four "; var results = test.match(re); The results I expect are: [0]: "test " [1]: "one " [2]: "two " [3]: "three " [4]: "four " Ho...

Checking string format with regex in PHP

I have been trying to use preg_match with "/^[a-zA-Z0-9_-]+$/" trying to make sure that the string given only contained the characters there and nothing else, but it seems to not be counting the $ properly so that it matches all the way to the end of the line, but it does require the match at the start of the string using ^. If I use {3...

Regular expressions and php

Say I have a string like this: $string = 'The /*quick*/ brown /*fox*/ jumped over the lazy /*dog*/.'; How can I use a regular expression to find the occurrences of /* */ and replace each value like so: /*quick*/ with the value of $_POST['quick'] /*fox*/ with the value of $_POST['fox'] /*dog*/ with the value of $_POST['dog'] I have ...

struggling with conditional regular expressions

I have a simple problem, yet i am unable to solve this. Either my string has a format ID: dddd, with the following regular expression: /^ID: ([a-z0-9]*)$/ or as follows: ID: 1234 Status: 232, so with the following regular expression: /^ID: ([a-z0-9]*) Status: ([a-z0-9]*)$/ Now i want to make one regular expression that can handle b...

unexpected regex match

Hi, Can anyone explain why the following test is not passing. The regex is getting a match where I do not want one. I want to find a match on text that begins with Tel, Fax or Web but for some reason, the url in the test is getting a match: def test_url_should_match assert_no_match(/^[tel|fax|web]/i, "www.jehall.co.uk") end ...

How can I exclude some characters from a class?

Say I want to match a "word" character (\w), but exclude "_", or match a whitespace character (\s), but exclude "\t". How can I do this? ...

Regex to find all sentences of text ?

Hello everyone, I have been trying to teach myself Regexes in python and I decided to print out all the sentences of a text. I have been tinkering with the regular expressions for the past 3 hours to no avail. I just tried the following but couldn't do anything. p = open('anan.txt') process = p.read() regexMatch = re.findall('^[A-Z].+...