pcre

A preg_replace puzzle: replacing zero or more of a char at the end of the subject

Say $d is a directory path and I want to ensure that it starts and ends with exactly one slash (/). It may initially have zero, one or more leading and/or trailing slashes. I tried: preg_replace('%^/*|/*$', '/', $d); which works for the leading slash but to my surprise yields two trailing slashes if $d has at least one trailing slash...

regex int + comma

Hi, i need a regex for: 123,456,789,123,4444,... basically comma separated values. The INT part can be 1-4 numbers long, followed by a comma...always in this form... /^([0-9]{1,4})(\,)?$/ This obviously doesn't work... Thanks! ...

A regular expression to filter undesired usernames

On a site I am working on I have a requirement that usernames not start with <alpha><alpha>_ So these should not be allowed: SU_Coolguy MR_Nobody my_Pony But these should be ok: __SU_Coolguy MRS_Nobody YourPony In the framework I am using, I am only able to validate against matching regular expression, not non-matchi...

How to replace substring in string using pcre library ?

for example: (test)rrr(/test) -> (ll)rrr(/ll) regexp: ( (test)(.*?)(test) ) ...

regex pattern to match alternating subpatterns

Hi, I'm trying to devise a regex pattern (in PHP) which will allow for any alternation of two subpatterns. So if pattern A matches a group of three letters, and B matches a group of 2 numerals, all of these would be OK: aaa aaa66bbb 66 67abc 12abc34def56ghi78jkl I don't mind which subpattern starts or ends the sequence, just that af...

Eregi to preg_replace change for php 5.3 compatibility help

I have this line in one of my scripts and its throwing a deprecated error. eregi_replace( '\.([a-z]{3,4})$', "-{$width}x{$height}.\\1", $src ); Can someone show me how to turn this into preg_replace and tell me why and which bits of it need to change so I can learn for future changes? I have had a go myself but where this bit of code...

How to ignore whitespace and carriage returns within text using regular expressions

Hi, Im quite new to stackoverflow so I dont know if this question has been asked before, but I cant seem to find any past questions which hint at the answer. Any help is really appreciated and thanks in advance. I have this text: { 1282837200, -- [1] "Type", -- [2] "Name", -- [3] "Reason", -- [4] Amount, -- [5] }, -- [...

Optimized regex for N words around a given word (UTF-8)

I'm trying to find an optimized regex to return the N words (if available) around another one to build a summary. The string is in UTF-8, so the definition of "words" is larger than just [a-z]. The string that serves as the reference word could be in the middle of a word or not directly surrounded by spaces. I've already got the followi...

PHP mb_ereg_replace not replacing while preg_replace works as intended.

I am trying to replace in a string all non word characters with empty string expect for spaces and the put together all multiple spaces as one single space. Following code does this. $cleanedString = preg_replace('/[^\w]/', ' ', $name); $cleanedString = preg_replace('/\s+/', ' ', $cleanedString); But when I am trying to use mb_ereg...

Greedy Regex Matching

I'm trying to match a string that looks something like this: <$Fexample text in here>> with this expression: <\$F(.+?)>{2} However, there are some cases where my backreferenced content includes a ">", thus something like this: <$Fexample text in here <em>>> only matches example text in here <em in the backreference. What do I ne...

Extract numbers from text with regular expression

Hi, I am trying to extract 1 and 125 from this text with PHP: preg_match("/^(?P<digit>\d+)/", "1 Foo ($125)",$m) Can you please help? Thanks ...

Determine regular expression's specificity

Hi, Given the following regular expressions: - alice@[a-z]+\.[a-z]+ - [a-z]+@[a-z]+\.[a-z]+ - .* The string [email protected] will obviously match all three regular expressions. In the application I am developing, we are only interested in the 'most specific' match. In this case this is obviously the first one. Unfortunately th...

determine if regular expression only matches fixed-length strings

Hi, Is there a way of determining if the regular expression only matches fixed-length strings ? My idea would be to scan for *,+ and ? Then, some intelligent logic would be required to to look for {m,n} where m!=n. It is not necessary to take the | operator into account. Small example: ^\d{4} is fixed-length; ^\d{4,5} or ^\d+ are variab...

Why will this recursive regex only match when a character repeats 2^n - 1 times?

After reading polygenelubricants's series of articles on advanced regular expressions techniques (particularly How does this Java regex detect palindromes?), I decided to attempt to create my own PCRE regex to parse a palindrome, using recursion (in PHP). What I came up with was: ^(([a-z])(?1)\2|[a-z]?)$ My understanding of this expr...

How does this PCRE pattern detect palindromes?

This question is an educational demonstration of the usage of lookahead, nested reference, and conditionals in a PCRE pattern to match ALL palindromes, including the ones that can't be matched by the recursive pattern given in the PCRE man page. Examine this PCRE pattern in PHP snippet: $palindrome = '/(?x) ^ (?: (.) (?= ...

Upgradation of PCRE Library (Perl Compatible Regular Expressions) Support.

I have upgrade the (pcre-devel-6.6-1.1, pcre-6.6-1.1) support on test linux machine having centos-5.0. I have restarted the apache, But in php information it is still showing the old version of PCRE (PCRE Library Version-5.0 13-Sep-2004), I want to upgrade this version on machine please guide me to update this version. Thanks in advanc...

Avoiding processing special preg characters in replacement string

When using preg_replace() in PHP with strings generated at runtime, one can protect special regex characters (such as '$' or '+') in the search string by using preg_quote(). But what's the correct way to handle this in the replacement string? Take this code for example: <?php $haystack = '...a bit of sample text...'; $replacement = '\\...

What's the technical reason for "lookbehind assertion MUST be fixed length" in regex?

For example,the regex below will cause failure reporting lookbehind assertion is not fixed length: #(?<!(?:(?:src)|(?:href))=["\']?)((?:https?|ftp)://[^\s\'"<>()]+)#S Such kind of restriction doesn't exist for lookahead. ...

Regular expression to match a certain HTML element

I'm trying to write a regular expression for matching the following HTML. <span class="hidden_text">Some text here.</span> I'm struggling to write out the condition to match it and have tried the following, but in some cases it selects everything after the span as well. $condition = "/<span class=\"hidden_text\">(.*)<\/span>/"; If ...

How do I get PCRE to work with C++?

This is a newbie question but I hope I can express my question as clearly as possible. I'm trying to do pattern matching in C++. I've downloaded the Win32 version of PCRE from here and I've placed the downloaded pcre3.dll and pcreposix3.dll files into the folder of Dev-CPP's lib folder (I'm using Bloodshed Dev-C++ 4.9.9 IDE). I've a...