CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
how to split string without using split function

 



harshmane
Novice

Jun 22, 2011, 4:18 AM

Post #1 of 7 (3563 views)
how to split string without using split function Can't Post

i want to split given string on bases of space or any metacharacter not by using split function but by using simple Regex.


rovf
Veteran

Jun 22, 2011, 4:51 AM

Post #2 of 7 (3561 views)
Re: [harshmane] how to split string without using split function [In reply to] Can't Post


Quote
not by using split function but by using simple Regex.


The first argument to split *is* a regex.

However, doing it without 'split' would go like this:

Match your string on the pattern

/(X)/g

(note the g-modifier!) in list context. X is a pattern describing the fields you want to keep together. For example,


Code
      'xxabayyyybcbbbzz' =~ /([abc]+)/g


evaluated in list context, yields the list ('aba','bcbb').


harshmane
Novice

Jun 22, 2011, 9:26 AM

Post #3 of 7 (3558 views)
Re: [rovf] how to split string without using split function [In reply to] Can't Post

$str ="hi hi how are you hi hi";
$s1 ="hi";
now if i want to find no of times "hi" is repeated in $str i tried it its not working.


FishMonger
Veteran / Moderator

Jun 22, 2011, 9:40 AM

Post #4 of 7 (3555 views)
Re: [harshmane] how to split string without using split function [In reply to] Can't Post


Code
$str ="hi hi how are you hi hi"; 
$s1 ="hi";

$cnt = () = $str =~ /$s1/g;
print $cnt;



harshmane
Novice

Jun 22, 2011, 10:16 AM

Post #5 of 7 (3552 views)
Re: [FishMonger] how to split string without using split function [In reply to] Can't Post

what is the use of () which is between $cnt =()=$str... i want to know its function still i did not read refrence & module in perl.


(This post was edited by harshmane on Jun 22, 2011, 10:19 AM)


miller
User

Jun 22, 2011, 11:15 AM

Post #6 of 7 (3548 views)
Re: [harshmane] how to split string without using split function [In reply to] Can't Post

perlfaq4 - How can I count the number of occurrences of a substring within a string?

- Miller


rovf
Veteran

Jun 22, 2011, 12:20 PM

Post #7 of 7 (3544 views)
Re: [harshmane] how to split string without using split function [In reply to] Can't Post

Well, this is something different than you asked for in your initial posting (where you asked about spliting on a set of certain characters, including space). I think for a new subject, you should start a new thread.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives