prinzdezibel
prinzdezibel

Reputation: 11147

Get words delimited by "#$"

How can I match the three words in the following string with a Perl compatible regular expression?

word1#$word2#$word3

I don't know the actual words "word1, word2 and word3" in advance. I only know the separator, which is #$.

And I can't use the word boundary as I have a multibyte encoding. This means for instance that the string can contain non-ASCII characters like umlauts which are not detected by the \w control character.

Upvotes: -1

Views: 847

Answers (6)

Brad Gilbert
Brad Gilbert

Reputation: 34120

This will work for any string that has 2 #

/([^#]+)\#\$([^#]+)\#\$([^#]+)/

Upvotes: 0

eyelidlessness
eyelidlessness

Reputation: 63519

$str = explode('#$', $str);

Regex is overkill for this.

Upvotes: 1

Gumbo
Gumbo

Reputation: 655209

Try this regular expression:

/(\w+)#\$(\w+)#\$(\w+)/

Edit   After your provided us with more information (see the comments to this answer):

/((?:[^#]+|#[^$])*)#\$((?:[^#]+|#[^$])*)#\$((?:[^#]+|#[^$])*)/

Upvotes: 2

gnomed
gnomed

Reputation: 5565

A split function might be useful although it depends what you want to do with the line.

here is an example though.

my $line = "word1#$word2#$word3"
my @words = split('#$', $line)

Upvotes: 0

cdm9002
cdm9002

Reputation: 1960

/([^#]*?)#\$([^#]*?)#\$([^#]*)/

Upvotes: -2

Sinan Ünür
Sinan Ünür

Reputation: 118118

#!/usr/bin/perl

use strict;
use warnings;

my $x = 'word1#$word2#$word3';
print $_, "\n" for split /#\$/, $x;

Upvotes: 1

Related Questions