Perl parse a string and substitute multiple symbols
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've got a string:
$str = "$JVM/$JAVA8/Contents/Home"
On this string, I'd like to substitute the symbols $JVM and $JAVA8 with the corresponding keys from this hash:
%con = ('$JVM' => '/Library/Java/JavaVirtualMachines' ,
'$JAVA8' => 'jdk1.8.0_192.jdk');
So that I can get this:
$target = "/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home"
I'd like to do it without any modules. How can I do that? Thank you.
string perl substitution
add a comment |
I've got a string:
$str = "$JVM/$JAVA8/Contents/Home"
On this string, I'd like to substitute the symbols $JVM and $JAVA8 with the corresponding keys from this hash:
%con = ('$JVM' => '/Library/Java/JavaVirtualMachines' ,
'$JAVA8' => 'jdk1.8.0_192.jdk');
So that I can get this:
$target = "/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home"
I'd like to do it without any modules. How can I do that? Thank you.
string perl substitution
If your string is"$JVM/..." then you'll get a compile time error, because you don't have a
$JVM` variable declared. The double quotes""
make Perl interpolate variables. You probable don't have this string written in your code, but rather read it as input. That's something entirely different and changes the meaning if your question. Please be specific.
– simbabque
Nov 16 '18 at 16:12
I have that str read from %ENV, actually. And when read from it it comes as "$JVM/$JAVA8/Contents/Home" But that's not the problem. The problem is finding a practical way to substitute those symbols with key values from a hash table. Thank you.
– Romario
Nov 16 '18 at 16:21
add a comment |
I've got a string:
$str = "$JVM/$JAVA8/Contents/Home"
On this string, I'd like to substitute the symbols $JVM and $JAVA8 with the corresponding keys from this hash:
%con = ('$JVM' => '/Library/Java/JavaVirtualMachines' ,
'$JAVA8' => 'jdk1.8.0_192.jdk');
So that I can get this:
$target = "/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home"
I'd like to do it without any modules. How can I do that? Thank you.
string perl substitution
I've got a string:
$str = "$JVM/$JAVA8/Contents/Home"
On this string, I'd like to substitute the symbols $JVM and $JAVA8 with the corresponding keys from this hash:
%con = ('$JVM' => '/Library/Java/JavaVirtualMachines' ,
'$JAVA8' => 'jdk1.8.0_192.jdk');
So that I can get this:
$target = "/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home"
I'd like to do it without any modules. How can I do that? Thank you.
string perl substitution
string perl substitution
edited Nov 16 '18 at 18:19
ikegami
268k11181407
268k11181407
asked Nov 16 '18 at 16:08
RomarioRomario
336215
336215
If your string is"$JVM/..." then you'll get a compile time error, because you don't have a
$JVM` variable declared. The double quotes""
make Perl interpolate variables. You probable don't have this string written in your code, but rather read it as input. That's something entirely different and changes the meaning if your question. Please be specific.
– simbabque
Nov 16 '18 at 16:12
I have that str read from %ENV, actually. And when read from it it comes as "$JVM/$JAVA8/Contents/Home" But that's not the problem. The problem is finding a practical way to substitute those symbols with key values from a hash table. Thank you.
– Romario
Nov 16 '18 at 16:21
add a comment |
If your string is"$JVM/..." then you'll get a compile time error, because you don't have a
$JVM` variable declared. The double quotes""
make Perl interpolate variables. You probable don't have this string written in your code, but rather read it as input. That's something entirely different and changes the meaning if your question. Please be specific.
– simbabque
Nov 16 '18 at 16:12
I have that str read from %ENV, actually. And when read from it it comes as "$JVM/$JAVA8/Contents/Home" But that's not the problem. The problem is finding a practical way to substitute those symbols with key values from a hash table. Thank you.
– Romario
Nov 16 '18 at 16:21
If your string is
"$JVM/..." then you'll get a compile time error, because you don't have a
$JVM` variable declared. The double quotes ""
make Perl interpolate variables. You probable don't have this string written in your code, but rather read it as input. That's something entirely different and changes the meaning if your question. Please be specific.– simbabque
Nov 16 '18 at 16:12
If your string is
"$JVM/..." then you'll get a compile time error, because you don't have a
$JVM` variable declared. The double quotes ""
make Perl interpolate variables. You probable don't have this string written in your code, but rather read it as input. That's something entirely different and changes the meaning if your question. Please be specific.– simbabque
Nov 16 '18 at 16:12
I have that str read from %ENV, actually. And when read from it it comes as "$JVM/$JAVA8/Contents/Home" But that's not the problem. The problem is finding a practical way to substitute those symbols with key values from a hash table. Thank you.
– Romario
Nov 16 '18 at 16:21
I have that str read from %ENV, actually. And when read from it it comes as "$JVM/$JAVA8/Contents/Home" But that's not the problem. The problem is finding a practical way to substitute those symbols with key values from a hash table. Thank you.
– Romario
Nov 16 '18 at 16:21
add a comment |
1 Answer
1
active
oldest
votes
You can substitute to a variable. The left hand side of s///
can be a hash value.
my $str = '$JVM/$JAVA8/Contents/Home';
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{
( # capture group for $1
\ # the backslash in your '$JVM' hash key
$ # a literal dollar sign
[A-Z0-9_]+ # variable name
)
}{$con{$1}}gx;
print $str;
Note that in your $str
, you want to use single quotes ''
that do not do interpolation, or you'll get an error as you don't have a $JVM
variable.
At the same time, you need to pay attention to your hash keys. If they are really '$JVM'
and such, that means there's a literal backslash and a literal dollar
$
in them. Therefore in the pattern we need three backslashes \
. Two to get a literal one (as the backslash escapes things with special meanings in regular expressions), and one to escape the dollar.
If your keys do not have this backslash, the pattern changes.
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{($[A-Z0-9_]+)}{$con{$1}}g;
I used the /x
modifier in the first version, which allows comments and ignores whitespace. You don't have to do that.
Re "you'll get an error as you don't have a $JVM variable", No, but you'll get$JVM/...
instead of$JVM/...
.
– ikegami
Nov 16 '18 at 16:37
Thank you for the answer. I've tried it however my code contains some symbols which have '_' , e.g. like '$JAVA_HOME' and for those symbols it returns the same. I haven't talked about those other symbols to keep the question short.
– Romario
Nov 16 '18 at 16:47
@ikegami the question was edited. It was"$JVM/..."
initially, so my annotation was correct.
– simbabque
Nov 16 '18 at 16:57
@Romario do you know anything about regular expressions? You need to change the pattern to take underscores as well. Please try to understand the answer and the code in it rather than just blindly copying it. If you do that, you'll eventually copy and run bad code off the internet, and cause harm to your computer.
– simbabque
Nov 16 '18 at 16:58
Of course I know about regexes. I added _ inside the brackets but the $str still returned either the original value or some other wrong result. Btw, among the symbols there are ones like JAVA_HOME_ , JAVA8_
– Romario
Nov 16 '18 at 17:16
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341564%2fperl-parse-a-string-and-substitute-multiple-symbols%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can substitute to a variable. The left hand side of s///
can be a hash value.
my $str = '$JVM/$JAVA8/Contents/Home';
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{
( # capture group for $1
\ # the backslash in your '$JVM' hash key
$ # a literal dollar sign
[A-Z0-9_]+ # variable name
)
}{$con{$1}}gx;
print $str;
Note that in your $str
, you want to use single quotes ''
that do not do interpolation, or you'll get an error as you don't have a $JVM
variable.
At the same time, you need to pay attention to your hash keys. If they are really '$JVM'
and such, that means there's a literal backslash and a literal dollar
$
in them. Therefore in the pattern we need three backslashes \
. Two to get a literal one (as the backslash escapes things with special meanings in regular expressions), and one to escape the dollar.
If your keys do not have this backslash, the pattern changes.
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{($[A-Z0-9_]+)}{$con{$1}}g;
I used the /x
modifier in the first version, which allows comments and ignores whitespace. You don't have to do that.
Re "you'll get an error as you don't have a $JVM variable", No, but you'll get$JVM/...
instead of$JVM/...
.
– ikegami
Nov 16 '18 at 16:37
Thank you for the answer. I've tried it however my code contains some symbols which have '_' , e.g. like '$JAVA_HOME' and for those symbols it returns the same. I haven't talked about those other symbols to keep the question short.
– Romario
Nov 16 '18 at 16:47
@ikegami the question was edited. It was"$JVM/..."
initially, so my annotation was correct.
– simbabque
Nov 16 '18 at 16:57
@Romario do you know anything about regular expressions? You need to change the pattern to take underscores as well. Please try to understand the answer and the code in it rather than just blindly copying it. If you do that, you'll eventually copy and run bad code off the internet, and cause harm to your computer.
– simbabque
Nov 16 '18 at 16:58
Of course I know about regexes. I added _ inside the brackets but the $str still returned either the original value or some other wrong result. Btw, among the symbols there are ones like JAVA_HOME_ , JAVA8_
– Romario
Nov 16 '18 at 17:16
|
show 1 more comment
You can substitute to a variable. The left hand side of s///
can be a hash value.
my $str = '$JVM/$JAVA8/Contents/Home';
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{
( # capture group for $1
\ # the backslash in your '$JVM' hash key
$ # a literal dollar sign
[A-Z0-9_]+ # variable name
)
}{$con{$1}}gx;
print $str;
Note that in your $str
, you want to use single quotes ''
that do not do interpolation, or you'll get an error as you don't have a $JVM
variable.
At the same time, you need to pay attention to your hash keys. If they are really '$JVM'
and such, that means there's a literal backslash and a literal dollar
$
in them. Therefore in the pattern we need three backslashes \
. Two to get a literal one (as the backslash escapes things with special meanings in regular expressions), and one to escape the dollar.
If your keys do not have this backslash, the pattern changes.
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{($[A-Z0-9_]+)}{$con{$1}}g;
I used the /x
modifier in the first version, which allows comments and ignores whitespace. You don't have to do that.
Re "you'll get an error as you don't have a $JVM variable", No, but you'll get$JVM/...
instead of$JVM/...
.
– ikegami
Nov 16 '18 at 16:37
Thank you for the answer. I've tried it however my code contains some symbols which have '_' , e.g. like '$JAVA_HOME' and for those symbols it returns the same. I haven't talked about those other symbols to keep the question short.
– Romario
Nov 16 '18 at 16:47
@ikegami the question was edited. It was"$JVM/..."
initially, so my annotation was correct.
– simbabque
Nov 16 '18 at 16:57
@Romario do you know anything about regular expressions? You need to change the pattern to take underscores as well. Please try to understand the answer and the code in it rather than just blindly copying it. If you do that, you'll eventually copy and run bad code off the internet, and cause harm to your computer.
– simbabque
Nov 16 '18 at 16:58
Of course I know about regexes. I added _ inside the brackets but the $str still returned either the original value or some other wrong result. Btw, among the symbols there are ones like JAVA_HOME_ , JAVA8_
– Romario
Nov 16 '18 at 17:16
|
show 1 more comment
You can substitute to a variable. The left hand side of s///
can be a hash value.
my $str = '$JVM/$JAVA8/Contents/Home';
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{
( # capture group for $1
\ # the backslash in your '$JVM' hash key
$ # a literal dollar sign
[A-Z0-9_]+ # variable name
)
}{$con{$1}}gx;
print $str;
Note that in your $str
, you want to use single quotes ''
that do not do interpolation, or you'll get an error as you don't have a $JVM
variable.
At the same time, you need to pay attention to your hash keys. If they are really '$JVM'
and such, that means there's a literal backslash and a literal dollar
$
in them. Therefore in the pattern we need three backslashes \
. Two to get a literal one (as the backslash escapes things with special meanings in regular expressions), and one to escape the dollar.
If your keys do not have this backslash, the pattern changes.
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{($[A-Z0-9_]+)}{$con{$1}}g;
I used the /x
modifier in the first version, which allows comments and ignores whitespace. You don't have to do that.
You can substitute to a variable. The left hand side of s///
can be a hash value.
my $str = '$JVM/$JAVA8/Contents/Home';
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{
( # capture group for $1
\ # the backslash in your '$JVM' hash key
$ # a literal dollar sign
[A-Z0-9_]+ # variable name
)
}{$con{$1}}gx;
print $str;
Note that in your $str
, you want to use single quotes ''
that do not do interpolation, or you'll get an error as you don't have a $JVM
variable.
At the same time, you need to pay attention to your hash keys. If they are really '$JVM'
and such, that means there's a literal backslash and a literal dollar
$
in them. Therefore in the pattern we need three backslashes \
. Two to get a literal one (as the backslash escapes things with special meanings in regular expressions), and one to escape the dollar.
If your keys do not have this backslash, the pattern changes.
my %con = (
'$JVM' => '/Library/Java/JavaVirtualMachines',
'$JAVA8' => 'jdk1.8.0_192.jdk'
);
$str =~ s{($[A-Z0-9_]+)}{$con{$1}}g;
I used the /x
modifier in the first version, which allows comments and ignores whitespace. You don't have to do that.
edited Nov 16 '18 at 18:18
ikegami
268k11181407
268k11181407
answered Nov 16 '18 at 16:18
simbabquesimbabque
44.5k757106
44.5k757106
Re "you'll get an error as you don't have a $JVM variable", No, but you'll get$JVM/...
instead of$JVM/...
.
– ikegami
Nov 16 '18 at 16:37
Thank you for the answer. I've tried it however my code contains some symbols which have '_' , e.g. like '$JAVA_HOME' and for those symbols it returns the same. I haven't talked about those other symbols to keep the question short.
– Romario
Nov 16 '18 at 16:47
@ikegami the question was edited. It was"$JVM/..."
initially, so my annotation was correct.
– simbabque
Nov 16 '18 at 16:57
@Romario do you know anything about regular expressions? You need to change the pattern to take underscores as well. Please try to understand the answer and the code in it rather than just blindly copying it. If you do that, you'll eventually copy and run bad code off the internet, and cause harm to your computer.
– simbabque
Nov 16 '18 at 16:58
Of course I know about regexes. I added _ inside the brackets but the $str still returned either the original value or some other wrong result. Btw, among the symbols there are ones like JAVA_HOME_ , JAVA8_
– Romario
Nov 16 '18 at 17:16
|
show 1 more comment
Re "you'll get an error as you don't have a $JVM variable", No, but you'll get$JVM/...
instead of$JVM/...
.
– ikegami
Nov 16 '18 at 16:37
Thank you for the answer. I've tried it however my code contains some symbols which have '_' , e.g. like '$JAVA_HOME' and for those symbols it returns the same. I haven't talked about those other symbols to keep the question short.
– Romario
Nov 16 '18 at 16:47
@ikegami the question was edited. It was"$JVM/..."
initially, so my annotation was correct.
– simbabque
Nov 16 '18 at 16:57
@Romario do you know anything about regular expressions? You need to change the pattern to take underscores as well. Please try to understand the answer and the code in it rather than just blindly copying it. If you do that, you'll eventually copy and run bad code off the internet, and cause harm to your computer.
– simbabque
Nov 16 '18 at 16:58
Of course I know about regexes. I added _ inside the brackets but the $str still returned either the original value or some other wrong result. Btw, among the symbols there are ones like JAVA_HOME_ , JAVA8_
– Romario
Nov 16 '18 at 17:16
Re "you'll get an error as you don't have a $JVM variable", No, but you'll get
$JVM/...
instead of $JVM/...
.– ikegami
Nov 16 '18 at 16:37
Re "you'll get an error as you don't have a $JVM variable", No, but you'll get
$JVM/...
instead of $JVM/...
.– ikegami
Nov 16 '18 at 16:37
Thank you for the answer. I've tried it however my code contains some symbols which have '_' , e.g. like '$JAVA_HOME' and for those symbols it returns the same. I haven't talked about those other symbols to keep the question short.
– Romario
Nov 16 '18 at 16:47
Thank you for the answer. I've tried it however my code contains some symbols which have '_' , e.g. like '$JAVA_HOME' and for those symbols it returns the same. I haven't talked about those other symbols to keep the question short.
– Romario
Nov 16 '18 at 16:47
@ikegami the question was edited. It was
"$JVM/..."
initially, so my annotation was correct.– simbabque
Nov 16 '18 at 16:57
@ikegami the question was edited. It was
"$JVM/..."
initially, so my annotation was correct.– simbabque
Nov 16 '18 at 16:57
@Romario do you know anything about regular expressions? You need to change the pattern to take underscores as well. Please try to understand the answer and the code in it rather than just blindly copying it. If you do that, you'll eventually copy and run bad code off the internet, and cause harm to your computer.
– simbabque
Nov 16 '18 at 16:58
@Romario do you know anything about regular expressions? You need to change the pattern to take underscores as well. Please try to understand the answer and the code in it rather than just blindly copying it. If you do that, you'll eventually copy and run bad code off the internet, and cause harm to your computer.
– simbabque
Nov 16 '18 at 16:58
Of course I know about regexes. I added _ inside the brackets but the $str still returned either the original value or some other wrong result. Btw, among the symbols there are ones like JAVA_HOME_ , JAVA8_
– Romario
Nov 16 '18 at 17:16
Of course I know about regexes. I added _ inside the brackets but the $str still returned either the original value or some other wrong result. Btw, among the symbols there are ones like JAVA_HOME_ , JAVA8_
– Romario
Nov 16 '18 at 17:16
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341564%2fperl-parse-a-string-and-substitute-multiple-symbols%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If your string is
"$JVM/..." then you'll get a compile time error, because you don't have a
$JVM` variable declared. The double quotes""
make Perl interpolate variables. You probable don't have this string written in your code, but rather read it as input. That's something entirely different and changes the meaning if your question. Please be specific.– simbabque
Nov 16 '18 at 16:12
I have that str read from %ENV, actually. And when read from it it comes as "$JVM/$JAVA8/Contents/Home" But that's not the problem. The problem is finding a practical way to substitute those symbols with key values from a hash table. Thank you.
– Romario
Nov 16 '18 at 16:21