regex

Is it better to use look-behind or capture groups?

I'm not sure if one of these is 'better' then the other, and why it would be, but I've got an original string that looks like this: $string = '/random_length_user/file.php'; Now, there are two ways to match it, the first, using my new friend, the look-behind, and the 2nd, without: preg_match("%(?<=^/)([^/]*)%", $string, $capture); pr...

Using php and regex to grab labels and data and store as associative array

How could i use regex to find this table in a page (need to find it by name): <table id="Table Name"> <tr><td class="label">Name:</td> <td class="data"><div class="datainfo">Stuff</div></td></tr> <tr><td class="label">Email:</td> <td class="data"><div class="datainfo">Stuff2</div></td></tr> <tr><td class="label">Address:</td> <td class=...

Distance between regular expression

Can we compute a sort of distance between regular expressions ? The idea is to mesure in which way two regular expression are similar. ...

How can I replace text that is not part of an anchor tag in Perl?

What is a Perl regex that can replace select text that is not part of an anchor tag? For example I would like to replace only the last "text" in the following code. blah <a href="http://www.text.com"&gt; blah text blah </a> blah text blah. Thanks. ...

How can I match certain nested parentheses in Perl?

^\s*[)]*\s*$ and ^\s*[(]*\s*$ matches the parentheses ( and ) which are bold. That is, what am trying is to ignore parentheses that are single and not (condition1) parentheses: while ( #matches here ( #matches here (condition1) && (condition2) && condition3 ) || (#matches here (condition4) || ...

regular expression

(.[^_]+) Matches correctly when there is no underscore, how can I modify this regex to match when there is no underscore only before a question mark ? ie. ignore any underscores after ? ...

Filtering out bad characters using a Regular Expression

I want to filter out the char ^ before searching something in a database. What will my regular expression look like if i want to achieve that the query will ignore this sign: ^ ? I'm working with VS2008 .net 3.5 and C#. ...

How preg_match works exactly?

Hi, I've wrote a simple function to check if the string I send "should be" valid or not. // this works without problems function validate_email ($value) { return preg_match ("/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/", $value); } // this doesn't work function validate_string ($value) { retu...

How do I get a random result from a preg_match_all ?

(Sorry if the title is pretty useless) I have this function to get the first image from a random post in WordPress. This works great, but now I need it to select a random image from all the matches, rather than the first. (I'm running this function in a query_posts loop to select the categories) // Get first image in post function catc...

PHP: PREG: How to match special chars like a grave?

Hi, I'd like to give my users the option to not only fill in letters and numbers, but also "special" letters like the "á", "é" etc. Though I do not want them to be able to use symbols like "!", "@", "%" etc. Is there a way to write a regex to accomplish this? (preferably without specifying each special letter) Now I have; $reg = '/^[...

Java - Tokenize Parameter List

I'm trying to create a method which takes a String parameter and then returns a two dimensional String array of parameter names and values. protected final String[][] setParams (String parms) { String[][] params; int i = 0; Pattern p = Pattern.compile(NEED_REGEX_HERE); Matcher m = p.matcher(parms); params = String[m...

Rewrite .htaccess regex url

I have an .htaccess file with this: (I didn't write it) RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?bla=$1 [L,QSA] Problem URL http://localhost/index/test The "index" part seems to match "index.php" that's in a web dir and Rewrite fails. Question: What's wron...

Basic Regexp Question: How to modify only the lines starting with a STRING with sed

I have to replace all ocurrences of: 5.6xx and 5.5xx (where x is a 0-9 digit) on a textfile with 5.500, but only when the line that contains the match starts with a string (e.g. STARTSTRING). That means STARTSTRING 5.610 4.500 3.550 5.530 OTHERSTRING 5.600 5.500 5.500 5.600 should become STARTSTRING 5.500 4.500 3.550 5.500 OTHERSTRIN...

How can I replace < and > in the content of xml file using regex?

How can i replace a "<" and a ">" (in the content of xml file) with a matching "&lt;" and "&gt;" (with a pre known set of tags) using a regex? example: <abc>fd<jkh</abc><def>e>e</def> should result with: <abc>fd&lt;jkh</abc><def>e&lt;e</def> it must be done with a regex! (no xml load and such...) ...

.NET Regex Not working as it should

I have the following regex pattern: (.NET 1.1 Regex Validator) ^(?=.*[A-Za-z])[a-zA-Z0-9@\\-_\\+\\.]{6,32}$ I need to meet the following requirements: 6 to 32 characters must contain at least one letter. Allowed characters are letters (a-z, A-Z), numbers (0-9), @ ("at" symbol), . (period), _ (underscore), + (plus), - (minus)...

replacing an undefined tags inside an xml string using a regex.

Hi! i need to replace an undefined tags inside an xml string. example: <abc> <>sdfsd <dfsdf></abc><def><movie></def> (only <abc> and <def> are defined) should result with: <abc> &lt;&gt;sdfsd &lt;dfsdf&gt;</abc><def>&lt;movie&gt;<def> <> and <dfsdf> are not predefined as and and does not have a closing tag. it must be done with a r...

Why does my regular expression fail with certain substitutions?

I am new to perl and not sure how to achieve the following. I am reading a file and putting the lines in a variable called $tline. Next, I am trying to replace some character from the $tline. This substitution fails if $tline has some special characters like (, ?,= etc in it. How to escape the special characters from this variable $tline...

Check SQL statements within PHP for bad words such as DROP or DELETE

Hi! I would like to allow some admins to manually enter SQL statements in a textfield (to get statistic data etc.). On the database layer, I protected the data by creating a user which can only select but not update/delete etc. I would like to add a second security by checking the inserted SQL for bad words such as DROP, DELETE or UPDA...

import phone numbers from a string in vb.net

There has got to be an easier way to do this. I am trying to wirte a function for a Phone number class called "import phone number". It should take any string with 10 digits in it somewhere (and allow for an extension), and import them into it's own properties: AreaCode, Prefix, Suffix, and Extension (aaa-ppp-ssss-xxxx...). I check the...

Matching one-line JavaScript comments (//) with re

I'd like to filter out (mostly one-line) comments from (mostly valid) JavaScript using python's re module. For example: // this is a comment var x = 2 // and this is a comment too var url = "http://www.google.com/" // and "this" too url += 'but // this is not a comment' // however this one is url += 'this "is not a comment' + " and ' ne...