Sitemap    
  Home Food Photographs IT Parenting Resources Learning  
PERL
How to Read and write a file
Regular expression
Regular expression

regularexpression.pl
#need data2.txt
#23 1
#24523 1
#67 1
#55 1 3

open FILE, "+<", "data2.txt" or die "Cannot open file" ;
my @lines = <FILE>;
foreach (@lines)
{
  #selects '55 1 3'
  if($_ =~ m/ 1 3/)
  {
     print "check characters\n";
     print $_ ;
  }

  #only selects 23 1 and not 24523 1
  if($_ =~ /^23/)
  {
     print "check start of the line and characters\n";
     print $_ ;
  }
}
close FILE;

print "\n\n";
# How to select a word at end of the line
# This will select word 'array' only at the end of string
@myarray = ("How array can be called array" =~ m/([aA-zZ]+$)/g);
print join("::",@myarray);

print "\n\n";
# how to get first whole number
# d matches digit and + after that matches till next char is not number
# $1 is automatically created by PERL
if("2010/04/11" =~ m/(\d+)/)
{
  print "first number ".$1
}

print "\n\n";
# how to get all whole numbers in an array
@myarray = ("2010/04/11" =~ m/(\d+)/g);
print join("::",@myarray);

print "\n\n";
#How to get all whole words in an array
#[aA-zZ] matches alphabet, + after that matches till next char is not alphabet
@myarray = ("How 11 to get 33 words 44 in an array" =~ m/([aA-zZ]+)/g);
print join("::",@myarray);

print "\n\n";
# How to replace a matching string
#s/string to match/string to replace/
my $mystring = "replace me";
$mystring =~ s/replace/replaced/;
print $mystring;

print "\n\n";
# How to replace a matching string - case insensitive
#s/string to match/string to replace/i
my $mystring = "Replace me";
$mystring =~ s/replace/Replaced/i;
print $mystring;

Output
$ perl regularexpression.pl
check start of the line and characters
23 1
check characters
55 1 3


array

first number 2010

2010::04::11

How::to::get::all::whole::words::in::an::array

replaced me

Replaced me

ADD TO DEL.ICIO.US ADD TO DIGG ADD TO FURL ADD TO REDDIT ADD TO STUMBLEUPON ADD TO TECHNORATI FAVORITES ADD TO SQUIDOO ADD TO YAHOO MYWEB ADD TO ASK ADD TO GOOGLE
Post your comments:
Your Name: 
Security check:
Your Comment: 1500 chars
Latest
Wheat Cookies
WheatCookie
Wheat cookies is a healthy snack for your fussy toddler......
Babyfood  2010-Aug-26
Wheat Cookies
WheatCookie
Wheat cookies is a healthy snack for your fussy toddler......
Veg Recipes Baking  2010-Aug-26
Encoding special characters in userinput or on server
Encoding can be done either in Javascript or JAVA encodeURIComponent, escape, java.net.URLEncoder..
Java JSP  2010-Aug-09
Jstl fmt tag i18n formatdate formatcurrency
fmt:setBundle, native2ascii.exe, fmt:message, fmt:setLocale, fmt:formatDate, fmt:formatNumber type=currency..
Java JSP  2010-Aug-05
How to read and write a file
How to read and write a file java.io.BufferedReader,java.io.FileReader, InputStreamReader ...
Java J2SE  2010-Aug-04
jQuery validate form using ajax 2
How to validate/submit form using ajax and jQuery input#, ($.ajax)..
Javascript  2010-Jul-23
jQuery validate form using ajax 1
How to validate/submit form using ajax and jQuery ($.ajax)..
Javascript  2010-Jul-23
Dosa
Dosa batterDosa
Masala dosa is my favourite South Indian dish and I relish it it whenever we go to a South Indian food joint......
Veg Recipes Parathas  2010-Jul-14
Palak Sukka/Spinach Sukka/Palakachi Sukhi Bhaji
Spinach leaves/images/food/vegrecipes/160X120/PALAK METHI SUKHI BHAJI 2 (2).JPG
If you are looking for a palak recipe apart from aloo palak or palak paneer, which quick yet delicious you are at the right place......
Veg Recipes Dry Veg  2010-Jul-13
How to create datasource in RAD Websphere
Create JDBC provider, datasource and JAAS security setup, JDBC connection URLs
Servers Websphere  2010-Jul-08
Get Started 4
Struts2 framework structure, how various components fit together
Java Struts2  2010-Jul-07
How to populate a form when JSP is called first time
This is achieved throught setting bean in request, using frameworks like Struts2, Spring3..
Java JSP  2010-Jul-07
Spring3 And Hibernate 4
Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
Java Spring  2010-Jul-07
Spring3 And Hibernate 3
Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
Java Spring  2010-Jul-07
Spring3 And Hibernate 1
Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
Java Spring  2010-Jul-07
Spring3 And Hibernate 2
Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
Java Spring  2010-Jul-07