1

I need to write a perl regex to convert

site.company.com => dc=site,dc=company,dc=com

Unfortunately I am not able to remove the trailing "," using the regex I came with below. I could of course remove the trailing "," in the next statement but would prefer that to be handled as a part of the regex.

$data="site.company.com";
$data =~ s/([^.]+)\.?/dc=$1,/g;
print $data;

This above code prints:

dc=site,dc=company,dc=com,

Thanks in advance.

4
  • "This above code prints: dc=site,dc=company,dc=com," That's exactly what you want, isn't it? Commented Feb 8, 2013 at 12:34
  • "dc=site,dc=company,dc=com" - Without the trailing ',' Commented Feb 8, 2013 at 12:34
  • Ooh yeah sorry, overlooked that. Commented Feb 8, 2013 at 12:34
  • You can add one more line after your code chop($data); Commented Feb 8, 2013 at 12:53

5 Answers 5

4

When handling urls it may be a good idea to use a module such as URI. However, I do not think it applies in this case.

This task is most easily solved with a split and join, I think:

my $url = "site.company.com";
my $string = join ",",            # join the parts with comma
             map "dc=$_",         # add the dc= to each part
             split /\./, $url;    # split into parts
Sign up to request clarification or add additional context in comments.

Comments

2
$data =~s/\./,dc=/g&&s/^/dc=/g;

tested below:

> echo "site.company.com" | perl -pe 's/\./,dc=/g&&s/^/dc=/g'
dc=site,dc=company,dc=com

4 Comments

Ah. +1 for Multiple substitution. My question is theoretical in the sense that I am looking to achieve this with a single regex statement and without using sed style substitution.
$data =~s/\./,dc=/g&&s/^/dc=/g; -- the first substitution will be applied to $data, but the second to $_.
But how does it matter when the final out come is what that is required?And @TLP i like your solution.
@sarathi The final outcome is not what is required. You perform the two substitutions on different variables in your first example. The second example is not the same as the first. Thank you. It seems the OP wanted a regex solution, though.
1

Try doing this :

my $x = "site.company.com";
my @a = split /\./, $x;
map { s/^/dc=/; } @a;
print join",", @a;

Comments

0

just put like this,

$data="site.company.com";
$data =~ s/,dc=$1/dc=$1/g; #(or) $data =~ s/,dc/dc/g;
print $data;

Comments

0

I'm going to try the /ge route:

$data =~ s{^|(\.)}{
    ( $1 && ',' ) . 'dc='
}ge;

e = evaluate replacement as Perl code.

So, it says given the start of the string, or a dot, make the following replacement. If it captured a period, then emit a ','. Regardless of this result, insert 'dc='.

Note, that I like to use a brace style of delimiter on all my evaluated replacements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.