Capturing regex of perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing regex of perl
# 1  
Old 12-17-2009
Capturing regex of perl

Hi all

I am struggling to find out the capturing regex of a date format such as 10/12/2009. Also I need help on how to assign the date(i.e, 10/12/2009 ) to a variable after the match is found using the capturing regex.
Any help is appreciated. Thanks in advance.
# 2  
Old 12-17-2009
Try

Code:
$_ = "Today is 17/12/2009  Tuesday";
print "Date is $1" if /([0-3][0-9]\/[0-1][0-9]\/[0-9[0-9][0-9][0-9])/;

# 3  
Old 12-17-2009
Thanks a lot it worked. But I am facing another problem when I want to assign another date formats (i.e., July 1, 1887 and 2000.10.17) in the similar fashion to $2 in continuation of first problem. Any help please.

Last edited by my_Perl; 12-17-2009 at 07:21 AM.. Reason: Problem modified
# 4  
Old 12-17-2009
Hi , I suggest you to check http://www.perl.com/lpt/a/718
Regards
# 5  
Old 12-17-2009
I am unable to capture the second date format with $2 using the regex. However it works only with $1. My problem on using multiple variables $1, $2 etc for different formats. Please help.
# 6  
Old 12-17-2009
Code:
$
$ cat -n dates.txt
     1  12/17/2009
     2  July 1, 1887
     3  2000.10.17
$
$ ##
$ perl -lne 'print;
>            if (/(\d{2})\/(\d{2})\/(\d{4})/){
>              printf("\tDay   = %s\n\tMonth = %s\n\tYear  = %s\n",$2,$1,$3)}
>            elsif (/(\w+) (\d+), (\d{4})/) {
>              printf("\tDay   = %s\n\tMonth = %s\n\tYear  = %s\n",$2,$1,$3)}
>            elsif (/(\d{4})\.(\d{2})\.(\d{2})/) {
>              printf("\tDay   = %s\n\tMonth = %s\n\tYear  = %s\n",$3,$2,$1)}
>           ' dates.txt
12/17/2009
        Day   = 17
        Month = 12
        Year  = 2009
July 1, 1887
        Day   = 1
        Month = July
        Year  = 1887
2000.10.17
        Day   = 17
        Month = 10
        Year  = 2000
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl REGEX help

Experts - I found a script on one of the servers that I work on and I need help understanding one of the lines. I know what the script does, but I'm having a hard time understanding the grouping. Can someone help me with this? Here's the script... #!/usr/bin/perl use strict; use... (2 Replies)
Discussion started by: timj123
2 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Regex - Capturing groups

I am having trouble with regex capturing groups, For Ex : I am having a file with ABC CDLF SFSDFK PRIMARY INDEX(XYZ,DEF,GHI); XYZ FLJ SDFKLD; PRIMARY INDEX(ABC); BHI SDKFLFLSFD PRIMARY INDEX (QWE , RTY , LHJ); My output should be : ABC XYZ,DEF,GHI XYZ ABC BHI ... (10 Replies)
Discussion started by: ysvsr1
10 Replies

4. Programming

Perl regex

Hello, I'm trying to get a quick help on regex since i'm not a regular programmer. Below is the line i'm trying to apply my regex to..i want to use the regex in a for loop and this line will keep on changing. subject=... (4 Replies)
Discussion started by: jhamaks
4 Replies

5. Programming

Perl regex

HI, I'm new to perl and need simple regex for reading a file using my perl script. The text file reads as - filename=/pot/uio/current/myremificates.txt certificates=/pot/uio/current/userdir/conf/user/gamma/settings/security/... (3 Replies)
Discussion started by: jhamaks
3 Replies

6. Shell Programming and Scripting

Perl : not capturing all the data from excel sheet

Hi folks, I am working on assignment that captures all the records(2 columns one column contains names and other contain date of birth) from excel sheet stored in a directory and checks for current date and month. If it matches current date and month then the matched records are printed as... (1 Reply)
Discussion started by: giridhar276
1 Replies

7. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

8. Shell Programming and Scripting

date capturing regex and storing

Hi all I need help on how to store two or more date formates captured using regex from an input sentence in PERL ? For example, I have an input sentence consisting of two dates such as : The departure date is August 12, 2009 and arrival date is 20.08.2009. Now, I want to capture the two... (4 Replies)
Discussion started by: my_Perl
4 Replies

9. Shell Programming and Scripting

capturing C++ binary return status in perl

Hello, I have a C++ binary that runs in my perl script. But, Currently, the binary is doing a core dump and i want to capture the retrun status of the binary to report as an issue. Can you please help me on this. Thanks, Sateesh (1 Reply)
Discussion started by: kotasateesh
1 Replies

10. Shell Programming and Scripting

capturing C++ binary return status in perl

Hello, I have a C++ binary that runs in my perl script. But, Currently, the binary is doing a core dump and i want to capture the retrun status of the binary to report as an issue. Can you please help me on this. Thanks, Sateesh (1 Reply)
Discussion started by: kotasateesh
1 Replies
Login or Register to Ask a Question