Perlfect Solutions
 

Perl Regex Tutorial

The Perl Regex Tutor shows you what a pattern will match against a string you provide. Start by entering a regex and some input in the boxes below and clicking the run button.
Regex:
Input:
Case-insensitive

Result

The matching part of the input appears in bold. Spaces are shown as ⋅. Capture groups are highlighted.

R e g e x T u t o r

Capture groups

You may turn capture groups on and off by clicking on the colored boxes.

Reg
T

Digg! Save This Page

Comments

Your name:
Your comments:

Security check *

 

Carlos   

Posted at 7:27am on Monday, August 27th, 2007

Very usefull

Carlos   

Posted at 7:31am on Monday, August 27th, 2007

Why this regex "^-\(w+)" does not retreive enything from here "-lolo",

Thank you very much

Perlfect   

Posted at 7:35am on Monday, August 27th, 2007

Carlos, the backslash should be before the "w+", not the parenthesis. Your regex should read "^-(\w+)", without the quotes.

Eddie   

Posted at 12:51pm on Tuesday, September 11th, 2007

I put (/w+) in your tutorial running on \fredupperlower/clipped/file.txt and it matches /clipped, which I want it to. But in my script, it's not working. The sequence is:

$dir = $File::Find::dir;
$dir =~ /(/w+)/;
$dirname = $1;
print "directory name: $dirnamen";

Eddie   

Posted at 1:16pm on Tuesday, September 11th, 2007

I tried using:

$dir =~ m!/(w+)!;

and that seemed to work (better than I expected, since it returns "clipped" instead of "/clipped" which is easier to work with for my purposes).

Agus   

Posted at 10:19am on Thursday, September 13th, 2007

I need your, how to validate phone number. eg: (021)087655 or 021-56745.

thanks

Sleve   

Posted at 6:53pm on Monday, September 24th, 2007

Agus: (d{3})d{6} or
d{3}-d{5}
I forget how to make it so that "either" will grab the line.
(((d{3})d{6})|(d{3}-d{5})) should have done it, I think, but it failed to catch the second one in capture groups.

Sleve   

Posted at 6:56pm on Monday, September 24th, 2007

Agus: I put it in correctly, but the comment engine stripped out some back slashes. There should be a backslash before each parenthesis in the first one listed.

Kokok   

Posted at 12:30am on Thursday, January 10th, 2008

This page is great to test my regex

Siidheesh   

Posted at 11:03pm on Friday, October 24th, 2008

Now that my regex, (TEXT|MATH)[_](.+)[(](.+)[)], is working correctly, how do I enclose the regex in an 'if' statement to test a variable against it and get the $1, $2 and $3 variables.

Karyn   

Posted at 8:38am on Monday, November 17th, 2008

Excellent - saved me a lot of time from running the script, changing the regexpr, running it again, re-changing the regexpr etc. Thanks!

Dennis   

Posted at 9:32pm on Wednesday, December 31st, 2008

Question: How do I validate the 'regular expression' itself before using it? I have an application that allows users to enter regular expressions to 'mask' data and it blows up if they enter an invalid regular expression. Would like to check the regex first for validity to prevent it from blowing up the script when it is actually used... Thanks in advance, Dennis

thyme   

Posted at 10:45am on Tuesday, January 13th, 2009

Which regular expression can I use to extract the FOUR-DIGIT number in the text "my name is mgt, I have 1234 at someplace"

Please help!!!

Score_Under   

Posted at 1:25pm on Thursday, January 15th, 2009

@Thyme:

bd{4}b

Score_Under   

Posted at 1:25pm on Thursday, January 15th, 2009

Sorry, it filtered the backslashes. There was a backslash before the "b", "d" and other "b".

soger   

Posted at 11:05am on Sunday, January 18th, 2009

Dennis: to validate a regular expression you can do something like this:

eval {''=~/$user_regex/};
if ($@) {
print "Invalid regular expression: $@";
} else {
do_something($user_regex);
}

alper   

Posted at 10:17am on Wednesday, February 4th, 2009

better than other online regex tools.. only thing is the text limit in box. is it really accepting only 36 characters ot is it my browser?

Steven the Easily Amused   

Posted at 10:25am on Monday, February 16th, 2009

Really Nice... just wish I could have a longer string as alper noted above.

vobb   

Posted at 10:25am on Monday, February 23rd, 2009

Nice. Simple, effective and visual.

Nandu   

Posted at 10:08pm on Sunday, April 26th, 2009

I need a clarofication for the following:-
For Input :cathatcatcat
Case 1)the regex ^(c|a|t|h)+$ results in the last "t" being matched
BUT
Case 2)^(c|a|t)+$ results in 0 matches
In (case 1) and (case 2) shouldn't the first "c" get matched?Please explain..

kuddus   

Posted at 9:51am on Monday, May 4th, 2009

fine

Scott   

Posted at 12:40pm on Thursday, May 7th, 2009

Thanks for the tool. It helped me to study for my final!

Benit   

Posted at 10:21am on Tuesday, June 2nd, 2009

I want to capture the first occurrent of AND in the following statement :
C:/Test/AND/Check/Run_AND/AND.exe

I want to replace from C: to first occurrence of AND by some other path.
Please help me.

Anonymous   

Posted at 4:54pm on Thursday, June 11th, 2009

This is great tool. Thanks for making it available. I have one feature request: I'd like to be able to test regex expressions larger than 40 characters.

keith   

Posted at 6:12pm on Wednesday, June 17th, 2009

Nandu: In the first case, your regex is saying "match if the first character of the input is 'c' or 'a' or 't' or 'h' and every subsequent character is one of those characters, and the last character is one of those characters. So your result matches (bold) for all characters in the first case, and the group (in green) ends matching the last character t.

In the second case, the first character is in the group, but with 'h' omitted, when it gets to the 'h' in the input, it fails and there is no match.

In other words, your regex is saying: match if the input is a string of characters made up of any combination of one or more of the characters in the group from beginning to end.

LCB   

Posted at 2:31am on Wednesday, July 1st, 2009

How I can get the string:
11x-A-20 (ST)
1as-50tdt
(Atx-220)

The string can vary from numbers and letters, and combine hyphens (1,2,3,n or none) and parenthesis...

from this lines:
1 11x-A-20 (ST) 831771.8700
1 1as-50tdt 831771.8700
1 (atx-220) 831771.8700

I tried several regex but have no luck to catch it at all
anyone had any idea how to catch the string part of this?

Comments to date: 26.

Like it? Share it!

  Post to del.icio.us
Post to
del.icio.us