I came across some regular expressions that I've never seen before, and I can't find any information on what they do. Here's an example:
/[\p{Z}\p{Cc}\p{Cf}\p{Cs}\p{Pi}\p{Pf}]/u
I'm looking for a full reference for regex.
P.S. I think the example provided only words in certain languages. It works in PHP but not Javascript.
The complete reference for PHP PCRE (Perl Compatible Regular Expression) is in the PHP docs.
What you're looking at are Unicode character properties, also in the PHP docs, as well as the regular expression modifiers for the u at the end of the regex.
Mastering Regular Expression 3rd is your best choice
Related
Hello All,
Thanks to #FailedDev I currently have the regex below which is used within a preg_match for a shoutbox. What I am trying to achieve in this question is allowing the regex to be case insensitive and give it the ability to allow the use of space(s) in the 'key word', which in this case is fred.
/(?<=^|\s)(?:\bfred\b|\$[$\w]*fred\b)/x
For background info please see the reference link.
Reference
Thank you for any help on this.
Update: Thanks to some helpful information, I have come up with the following regex that does what I need, though I feel it is not the most efficient solution.
~(?:(?<=\s|^)[$\S]*|\b)f+(?:\.+|\s+)?r+(?:\.+|\s+)?e+(?:\.+|\s+)?d+(?:\.+|)?\b~i
If you want to make it case insensitive, use the /i modifier.
To allow extra whitespace, use \s* for a variable number of whitespace characters, or [ ]? for a single optional space.
See also the manual on preg_match and the PCRE syntax overview and http://regular-expressions.info/ for a tutorial. Check also the reference question Is there anything like RegexBuddy in the open source world? for a list of tools to aid with crafting regular expressions. And some useful online tools.
I want to know how regex patterns are written. Where is their base and what do I need to know in order to write patterns. I have absolutely no idea on how to write my own patterns and I am quite in a need of finding a particular match in some of my code.
Is this some math based subject? Give me all possible information about patterns please :)
I would say you need to read the PCRE Patterns section of the manual -- and, more specificaly, the Pattern Syntax sub-section ;-)
Pretty much everything about PCRE is in there1.
Considering that PCRE are, afterall, Perl-Compatible Regular Expressions, you might also want to read some Perl-related documentation ; for exemple : perlre.
1. Well, once again, the PHP manual is so great, it wasn't possible to not link to it...
Pascal Martin already pointed to some useful online resources. If you're looking for a book, I can suggest Mastering Regular Expressions.
can somebody point me to a good regular expression resource (for php if it matters).
I am looking now for a book here amazon but don't know which one is better. It would be great to find something simple to understand and a fast and interesting process of learning.
You can start at http://www.regular-expressions.info/
The site also has a list of regular expressions books.
PHP supports several regular expressions variants, but the most important is PCRE (perl-comptabible regular expressions).
The mb_ereg family, besides supporting several encodings, also supports several variants:
j Java (Sun java.util.regex)
u GNU regex
g grep
c Emacs
r Ruby
z Perl
b POSIX Basic regex
d POSIX Extended regex
Mastering Regular Expressions by: Jeffrey E.F. Friedl is considered the bible of Regular Expressions books.
Also, as Artefacto mentioned: http://www.regular-expressions.info/ is a terrific resource with clear and simple explanations.
But the best way to learn is to play with them using a regex tool like Reggy (Mac tool)
Along with http://www.regular-expressions.info, you might find it useful to get an interactive regex editor.
The Regex Coach is a good, light-weight editor.
RegexBuddy is amazing, but costly.
regexpal.com a simple, online tester.
This is the only place I've found an intro to regular expressions that's suitable for beginners. It's part of an online book called "Practical PHP".
http://www.tuxradar.com/practicalphp/4/8/0
These are the only regex books you'll ever need:
Mastering Regular Expressions
Regular Expressions Cookbook
Both are great tools for learning regexes in general, but they both have lots of information specific to PHP as well.
Are regular expressions the same for PHP, MySQL, JavaScript, Perl, and so on? If so, is there a chart or tutorial that explains regular expressions?
No, there often are subtle differences in supported features (mostly of the pretty advanced kind1). For example, JavaScript regular expressions don't have lookbehind. PHP uses either POSIX extended regular expressions or PCRE (Perl-compatible regex), which are close to Perl's feature-set. In fact, Perl is probably the ancestor of many advanced features in today's regular expression engines.
As for tutorials and comparisons the site http://regular-expressions.info is a very good resource.
Once you got used to writing and applying them it often is helpful to just quickly try out things. I have found a REPL to be quite handy; I usually use Windows PowerShell but Ruby or Python are also pretty popular.
1 Thanks, Dancrumb.
In theory regular expression is a language for pattern matching.But there are little differences from language to language . My advice is use a tool like Regex Coach to building/learning regular expressions.
There is an excellent write-up of Perl regular expressions compared to "classic", POSIX and GNU grep in Chapter 3 of the book "Minimal Perl" by Tim Maher. And I think it's a good read for any of them, not just Perl.
And what do you know, "Sample Chapter 3" is available as a download from this page: Minimal Perl book!
is the regexp the same between languages?
for example. if i want to use it in javascript, would i have to search for regexp for javascript specifically. cause i got some cheat sheets. it just says regular expression.
i wonder if i could use this on all languages, php, javascript and so on.
The basics are mostly the same but there are some discrepancies between which engine powers the language, PHP and JavaScript differ since PHP uses PCRE (Perl Compatible Regular Expressions).
PHP also has the POSIX-compatible regex engine (ereg_* functions), but that is deprecated.
If you don't already use it, I suggest you try RegexBuddy. It can convert between several Regex engines.
You can find alternatives for RegexBuddy on Mac here.
You might want to start out by looking here. That's my Bible when I do regexping!
Now, regex should be the same everywhere, at least the fundamentals, however there are cases where it differs from compiler to compiler (or interpreter if you will).
Those could be how you search for a specific pattern, let's take \w as an example, that's: any alphanumeric or underscore character in c# but the pattern in javascript might be different.
When you come to a special case like this, you might want to revise the above provided link.
Regular expression synax varies slightly between languages but for the most part the details are the same. Some regex implementations support slightly different variations on how they process as well as what certain special character sequences mean.
Google is your best friend. Google for regex in the language of your choice.
One of the biggest variations in regex is how special characters are escaped / interpreted.
For instance, grep, vim and perl regexs differ in how to handle things like ( ) for grouping / capturing a pattern for back referencing in search & replace. IIRC, Perl uses them straight while grep and vim require them to be escaped.
Also, Perl regex may support more features than earlier regex engines. regex's that would have been simple in Perl were a major Pita in grep.
I'm not completely sure if this is a correct way to sum it up, but there are basically two major classes of regex - Posix ( grep and similar tools ) and Perl compatible ( with minor variations ).
One tool I've found useful is The Regex Coach - interactive regular expressions.