+ Reply to Thread
Results 1 to 2 of 2

Thread: Extract text seperated by "/" in perl

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    45

    Extract text seperated by "/" in perl

    So i have a text that looks like that :
    Code:
    $text = "a / b / c / d"
    $text = "1 / 2 / 3 / 4"
    $text = "name_1 / name+2 / name!@3 / name-_4"
    These were samples and simply text aren`t just A to Z , it can include numbers or special
    characters but not "/"
    So what is the best Pattern for that problem ?

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    34
    Okay so you can try that pattern :
    Code:
    $text = "name_1 / name+2 / name!@3 / name-_4"
    @values = split(" / ", $text );
    Output Should be :
    Code:
    "name_1"
    "name+2"
    "name!@3"
    "name-_4"
    And here is another solution that my friend suggested :
    Code:
    use strict;
    use warnings;
    
    my @text = (
    	"a / b / c / d",
    	"1 / 2 / 3 / 4",
    	"name_1 / name+2 / name!@3 / name-_4",
    );
    
    for (@text) {
    	my @fields = m|[^/\s](?:[^/]*[^/\s])?|g;
    	printf "(%s)\n",  join ', ', map "'$_'", @fields;
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts