regex

Retrieve number from the string pattern using regular expression

I have a string "Search result:16143 Results found", and I need to retrieve 16143 out of it. I am coding in ruby and I know this would be clean to get it using RegEx (as oppose to splitting string based on delimiters) How would I retrieve the number from this string in ruby? ...

Regular expression that matches between quotes, containing escaped quotes

This was originally a question I wanted to ask, but while researching the details for the question I found the solution and thought it may be of interest to others. In Apache, the full request is in double quotes and any quotes inside are always escaped with a backslash: 1.2.3.4 - - [15/Apr/2005:20:35:37 +0200] "GET /\" foo=bat\" HTTP/...

Javascript token replace/append

Hi guys I have a string that looks something like the following 'test:1;hello:five;just:23'. With this string I need to be able to do the following. .... var test = MergeTokens('test:1;hello:five;just:23', 'yes:23;test:567'); ... The end result should be 'test:567;hello:five;just:23;yes:23' (note the exact order of the tokens is not ...

Best way to implement conditional in a PHP templating system?

I'm creating a very simple PHP templating system for a custom CMS/news system. Each article has the intro and the full content (if applicable). I want to have a conditional of the form: {continue}click to continue{/continue} So if it's possible to continue, the text "click to continue" will display, otherwise, don't display this whole...

Parsing template schema with Python and Regular Expressions

I'm working on a script for work to extract data from an old template engine schema: [%price%] { $54.99 } [%/price%] [%model%] { WRT54G } [%/model%] [%brand%]{ LINKSYS } [%/brand%] everything within the [% %] is the key, and everything in the { } is the value. Using Python and regex, I was able to get this far: (?<=[%)(?P\w*?)(?=\%...

C# Regex Replace Question

if i have a string like this "Hello - World - Hello World" I want to replace the characters PRECEDING the FIRST instance of the substring " - " e.g. so replacing the above with "SUPERDOOPER" would leave: "SUPERDOOPER - World - Hello World" So far I got this: "^[^-]* - " But this INCLUDES the " - " which is wrong. how to do this wit...

Including new lines in PHP preg_replace function

I'm trying to match a string that may appear over multiple lines. It starts and ends with a specific string: {a}some string can be multiple lines {/a} Can I grab everything between {a} and {/a} with a regex? It seems the . doesn't match new lines, but I've tried the following with no luck: $template = preg_replace( $'/\{a\}([.\n]+)\{...

How do you ORDER by title in mysql, ignoring the word "the"?

I have a list of movies that I have grouped by letter. Naturally, the movies starting with the letter "T" have about 80% of movies that begin with "The". Movies such as "The Dark Knight" should appear in the "D" list, and preferably in the "T" as well. Any way I can do that? I use the following code in the WHERE clause to display movi...

php regex : get src value

How do I retrieve all src value using regex in php? <script type="text/javascript" src="http://localhost/assets/javascript/system.js" charset="UTF-8"></script> <script type='text/javascript' src='http://localhost/index.php?uid=93db46d877df1af2a360fa2b04aabb3c' charset='UTF-8'></script> The retrieved value should only contains: http:...

split string based on regexp

Hi, I want to split a string in C# that looks like a : b : "c:d" so that the resultant array will have Array[0] = "a" Array[1] = "b" Array[2] = "c:d" what regexp do I use to achieve the required result. Many Thanks ...

Using a MatchEvaluator delegate to generate a list without using class variables?

Background: I'm trying to generate a list of references from a page that has references throughout, and replace the reference markup with HTML. I'm working in C# Text looks something like: The dog ate 3 cats and felt ill <ref name="something">http://cateater.com&lt;/ref&gt; I'd like to use the Regex.Replace method to replace all the...

Regex to match multiple strings

I need to create a regex that can match multiple strings. For example, I want to find all the instances of "good" or "great". I found some examples, but what I came up with doesn't seem to work: \b(good|great)\w*\b Can anyone point me in the right direction? Edit: I should note that I don't want to just match whole words. For example...

What are some ways I can improve the performance of a regular expression query in Postgresql 8?

I'm performing a regular expression match on a column of type "character varying(256)" in postresql 8.3.3. The column currently has no indices. I'd like to improve the performance of this query if I can. Will adding an index help? Are there other things I can try to help improve performance? ...

regex help

I got an answer to my question here: http://stackoverflow.com/questions/699253/how-to-find-out-if-file-is-tab-or-space-delimited-in-perl/699256#699256 but it would really help me if someone could break down the regex and explain what is going on and why it wont work on the last line of the file. ~/^(\d+\s+)+?$/ I thought the above h...

Variable order regex syntax

Is there a way to indicate that two or more regex phrases can occur in any order? For instance, XML attributes can be written in any order. Say that I have the following XML: <a href="home.php" class="link" title="Home">Home</a> <a href="home.php" title="Home" class="link">Home</a> How would I write a match that checks the class and t...

RegEx to insert a string before each table in a MySQL query

I need to take a MySQL query and insert a string before each table name. The solution doesn't need to be one line but obviously it's a regex problem. It will be implemented in PHP so having programming logic is also fine. Rationale and Background: I'm revamping my code base to allow for table prefixes (eg: 'nx_users' instead of 'users'...

How to replace multiple newlines in a row with one newline using Ruby.

I have a script written in ruby. I need to remove any duplicate newlines (e.g.) \n \n \n to \n My current attempt worked (or rather not) using str.gsub!(/\n\n/, "\n") Which gave me no change to the output. What am I doing wrong? ...

Can you provide some examples of why it is hard to parse XML and HTML with a regex?

One mistake I see people making over and over again is trying to parse XML or HTML with a regex. Here are a few of the reasons parsing XML and HTML is hard: People want to treat a file as a sequence of lines, but this is valid: <tag attr="5" /> People want to treat < or <tag as the start of a tag, but stuff like this exists in the w...

preg_replace_callback() memory issue

i'm having a memory issue while testing a find/replace function. Say the search subject is: $subject = "I wrote an article in the A+ magazine. It'\s very long and full of words. I want to replace every A+ instance in this text by a link to a page dedicated to A+."; the string to be found : $find='A+'; $find = preg_quote($find...

How would one use System.Uri .NET class to check if the URL is valid?

In one of the previous posts it was suggested to use System.Uri to check if the URL is valid. How does one do that? ...