regex

Matched elements using regex in Perl

I have a hash which contains a regular expression: the number of matches to be captured in it and variables and their position of match. For example: my %hash = ( reg_ex => 'Variable1:\s+(.*?)\s+\n\s+Variable2:\s+(.*?)\s+\n', count => 2, Variable1 => 1, Variable2 => 2, ); I am going to use this regex in some other par...

Regex jquery

I want to validate a texbox with min char=5, max=20, allow alphabet and numbers and only 3 special characters !@# using plain jQuery (no plugin) function chkText() { $(".cssText").each(function() { // add regex condition here in an IF statement }); } ...

Is there a way, using regular expressions, to match a pattern for text outside of quotes?

As stated in the title, is there a way, using regular expressions, to match a text pattern for text that appears outside of quotes. Ideally, given the following examples, I would want to be able to match the comma that is outside of the quotes, but not the one in the quotes. This is some text, followed by "text, in quotes!" or ...

Regular Expression to Find/Replace in a Fix Length File

Hi, So I have this big file of fix length lines. I want to do a find and replace on a character line position. Example: xxxxxxx 010109 xxxxxx xxxxx xxxxxxx 010309 xxxxxx xxxxx xxxxxxx 021506 xxxxxx xxxxx xxxxxxx 041187 xxxxxx xxxxx So in this case I would want to find any value starting on position 13 through positio...

How to split string by ',' unless ',' is within brackets using Regex?

EDIT: Can anyone help me out with a regular expression for a string such as this?: [Header 1], [Head,er 2], Header 3 so that I can split this into chunks like: [Header 1] [Head,er 2] Header 3 I have gotten as far as this: (?<=,|^).*?(?=,|$) Which will give me: [Header 1] [Head ,er 2] Header 3 Thanks!! ...

skip over HTML tags in Regular Expression patterns

I'm trying to write a regular expression pattern (in python) for reformatting these template engine files. Basically the scheme looks like this: [$$price$$] { <h3 class="price"> $12.99 </h3> } I'm trying to make it remove any extra tabs\spaces\new lines so it should look like this: [$$price$$]{<h3 class="price">$12.99</h...

Replace Local Links, Keep External Links

Hi guys, I have an API call that essentially returns the HTML of a hosted wiki application page. I'm then doing some substr, str_replace and preg_replace kung-fu to format it as per my sites style guides. I do one set of calls to format my left nav (changing a link to pageX to my wikiParse?page=pageX type of thing). I can safely do thi...

How can I use URL rewriting to strip index.php from a URI?

Here's what I need- RegEx to match a url pattern and rewrite it to something else 301 redirect old pattern to new pattern Here's what I have- I have a url pattern that currently looks like this-- http://www.foo.com/press/index.php/2009/4-reasons-to-buy-now/ I want to create a pattern match on the url "http://www.foo.com/press/ind...

Regex and the "war" on XSS

I've always been interested in writing web software like forums or blogs, things which take a limited markup to rewrite into HTML. But lately, I've noticed more and more that for PHP, try googling "PHP BBCode parser -PEAR" and test a few out, you either get an inefficient mess, or you get poor code with XSS holes here and there. Taking...

String parsing, extracting numbers and letters

What's the easiest way to parse a string and extract a number and a letter? I have string that can be in the following format (number|letter or letter|number), i.e "10A", "B5", "C10", "1G", etc. I need to extract the 2 parts, i.e. "10A" -> "10" and "A". Update: Thanks to everyone for all the excellent answers ...

how to find and replace such a case

i have some text files in a pre-defined directory, the files end with *.edi Each of them refers in one separate line within itself: data_file file_n.data I have to convert this line in every .edi file to sth like data_file 'another_directory/file_n.data' i will add 'another_directory/ and ' in the end, because the data ...

Regular Expressions help?

Hey guys how can i expand this code: string BBCSplit = Regex.Replace(BBC, @"<(.|\n)*?>", string.Empty); how can i expand this to remove any special characters e.g. : ; , etc. but it still do what it does now which is remove div tags. I dont fully understand the syntax of regex's yet sorry! Thanks, Ash ...

Java's Scanner vs String.split() vs StringTokenizer; which should I use?

I am currently using split() to scan through a file where each line has number of strings delimited by '~'. I read somewhere that Scanner could do a better job with a long file, performance-wise, so I thought about checking it out. My question is: Would I have to create two instances of Scanner? That is, one to read a line and another ...

how to extract a portion of a string in php

I am using preg_replace() for some string replacement. $str = "<aa>Let's find the stuff qwe in between <id>12345</id> these two previous brackets</h>"; $do = preg_match("/qwe(.*)12345/", $str, $matches); which is working just fine and gives the following result $match[0]=qwe in between 12345 $match[1]=in between but I am using s...

Code to turn user input into a literal regular expression? (C#)

There are times when I want to convert user input into its literal value in a regular expression. I.e. if the user enters C:\Win\Bin\File.txt the regular expression would be something like C:\\Win\\Bin\File.txt since certain character combinations have to be escaped. Does anyone know of either a tried and true piece of code that does t...

How can I delete characters between < and > in Perl?

I need to write a Perl script to read in a file, and delete anything inside < >, even if they're on different lines. That is, if the input is: Hello, world. I <enjoy eating bagels. They are quite tasty. I prefer when I ate a bagel to when I >ate a sandwich. <I also like >bananas. I want the output to be: Hello, world. I ate a sandwic...

How to extract a substring from a .NET RegEx?

I have an XML file containing one (or more) key/value pairs. For each of these pairs I want to extract the value which is a two-byte hex value. So the XML contains this snippet: <key>LibID</key><val>A67A</val> Which I can match using the following expression, with the ID in parenthesis. Match match = Regex.Match(content, @"<key>LibI...

Apply multiple negative regex to expression in Python

This question is similar to "How to concisely cascade through multiple regex statements in Python" except instead of matching one regular expression and doing something I need to make sure I do not match a bunch of regular expressions, and if no matches are found (aka I have valid data) then do something. I have found one way to do it bu...

Why is it very slow for a Regex to finish a XML file of 3000 lines?

I noticed that it is very slow for a Regex to finish a XML file with 3000 lines [1]: \(<Annotation\(\s*\w\+="[^"]\{-}"\s\{-}\)*>\)\@<=\(\(<\/Annotation\)\@!\_.\)\{-}"MATCH\_.\{-}\(<\/Annotation>\)\@= I always thought that Regexes are efficient. Why does it take so long to finish the Regex? [1] http://stackoverflow.com/questions/7363...

When should I use a parser?

I have had problems in Regexes to divide a code up into functional components. They can break or it can take a long time for them to finish. The experience raises a question: "When should I use a parser?" ...