Regular expression for matching all variables in a shell script
up vote
1
down vote
favorite
Let's assume I have the following string:
cd $MY_DIR
This is some text:$MY_VARIABLE
Add some other text $1 $2 $FOO value=$BAR
What would be a valid regex to find all occurences of the variables?
I have tried the following expresion but it has some drawbacks as it:
- also selects the blank
- doesn't select
$1
$2
individually
$.*
regex visual-studio-code
|
show 3 more comments
up vote
1
down vote
favorite
Let's assume I have the following string:
cd $MY_DIR
This is some text:$MY_VARIABLE
Add some other text $1 $2 $FOO value=$BAR
What would be a valid regex to find all occurences of the variables?
I have tried the following expresion but it has some drawbacks as it:
- also selects the blank
- doesn't select
$1
$2
individually
$.*
regex visual-studio-code
What tool are you using?
– Wiktor Stribiżew
Nov 19 at 11:54
I'm using VS Code. Added the tag to my question.
– Robert Strauch
Nov 19 at 11:54
2
TryB$w+
....
– Wiktor Stribiżew
Nov 19 at 11:55
That would match$var
which aren't variable reference though. Maybe with an additional lookbehind, e.g.B(?<!\)$w+
?
– Aaron
Nov 19 at 11:58
1
@Aaron The lookbehind won't solve that anyway. One would need(?<!\)(?:\{2})*$[a-zA-Z_]w*
.
– Wiktor Stribiżew
Nov 19 at 11:58
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Let's assume I have the following string:
cd $MY_DIR
This is some text:$MY_VARIABLE
Add some other text $1 $2 $FOO value=$BAR
What would be a valid regex to find all occurences of the variables?
I have tried the following expresion but it has some drawbacks as it:
- also selects the blank
- doesn't select
$1
$2
individually
$.*
regex visual-studio-code
Let's assume I have the following string:
cd $MY_DIR
This is some text:$MY_VARIABLE
Add some other text $1 $2 $FOO value=$BAR
What would be a valid regex to find all occurences of the variables?
I have tried the following expresion but it has some drawbacks as it:
- also selects the blank
- doesn't select
$1
$2
individually
$.*
regex visual-studio-code
regex visual-studio-code
edited Nov 19 at 11:55
asked Nov 19 at 11:53
Robert Strauch
3,643105599
3,643105599
What tool are you using?
– Wiktor Stribiżew
Nov 19 at 11:54
I'm using VS Code. Added the tag to my question.
– Robert Strauch
Nov 19 at 11:54
2
TryB$w+
....
– Wiktor Stribiżew
Nov 19 at 11:55
That would match$var
which aren't variable reference though. Maybe with an additional lookbehind, e.g.B(?<!\)$w+
?
– Aaron
Nov 19 at 11:58
1
@Aaron The lookbehind won't solve that anyway. One would need(?<!\)(?:\{2})*$[a-zA-Z_]w*
.
– Wiktor Stribiżew
Nov 19 at 11:58
|
show 3 more comments
What tool are you using?
– Wiktor Stribiżew
Nov 19 at 11:54
I'm using VS Code. Added the tag to my question.
– Robert Strauch
Nov 19 at 11:54
2
TryB$w+
....
– Wiktor Stribiżew
Nov 19 at 11:55
That would match$var
which aren't variable reference though. Maybe with an additional lookbehind, e.g.B(?<!\)$w+
?
– Aaron
Nov 19 at 11:58
1
@Aaron The lookbehind won't solve that anyway. One would need(?<!\)(?:\{2})*$[a-zA-Z_]w*
.
– Wiktor Stribiżew
Nov 19 at 11:58
What tool are you using?
– Wiktor Stribiżew
Nov 19 at 11:54
What tool are you using?
– Wiktor Stribiżew
Nov 19 at 11:54
I'm using VS Code. Added the tag to my question.
– Robert Strauch
Nov 19 at 11:54
I'm using VS Code. Added the tag to my question.
– Robert Strauch
Nov 19 at 11:54
2
2
Try
B$w+
....– Wiktor Stribiżew
Nov 19 at 11:55
Try
B$w+
....– Wiktor Stribiżew
Nov 19 at 11:55
That would match
$var
which aren't variable reference though. Maybe with an additional lookbehind, e.g. B(?<!\)$w+
?– Aaron
Nov 19 at 11:58
That would match
$var
which aren't variable reference though. Maybe with an additional lookbehind, e.g. B(?<!\)$w+
?– Aaron
Nov 19 at 11:58
1
1
@Aaron The lookbehind won't solve that anyway. One would need
(?<!\)(?:\{2})*$[a-zA-Z_]w*
.– Wiktor Stribiżew
Nov 19 at 11:58
@Aaron The lookbehind won't solve that anyway. One would need
(?<!\)(?:\{2})*$[a-zA-Z_]w*
.– Wiktor Stribiżew
Nov 19 at 11:58
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You may use
B$w+
that matches a $
that is not preceded with a letter, digit or _
and then 1 or more letters, digits or _
.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You may use
B$w+
that matches a $
that is not preceded with a letter, digit or _
and then 1 or more letters, digits or _
.
add a comment |
up vote
2
down vote
accepted
You may use
B$w+
that matches a $
that is not preceded with a letter, digit or _
and then 1 or more letters, digits or _
.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You may use
B$w+
that matches a $
that is not preceded with a letter, digit or _
and then 1 or more letters, digits or _
.
You may use
B$w+
that matches a $
that is not preceded with a letter, digit or _
and then 1 or more letters, digits or _
.
answered Nov 19 at 12:06
Wiktor Stribiżew
301k16122197
301k16122197
add a comment |
add a comment |
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%2f53374088%2fregular-expression-for-matching-all-variables-in-a-shell-script%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
What tool are you using?
– Wiktor Stribiżew
Nov 19 at 11:54
I'm using VS Code. Added the tag to my question.
– Robert Strauch
Nov 19 at 11:54
2
Try
B$w+
....– Wiktor Stribiżew
Nov 19 at 11:55
That would match
$var
which aren't variable reference though. Maybe with an additional lookbehind, e.g.B(?<!\)$w+
?– Aaron
Nov 19 at 11:58
1
@Aaron The lookbehind won't solve that anyway. One would need
(?<!\)(?:\{2})*$[a-zA-Z_]w*
.– Wiktor Stribiżew
Nov 19 at 11:58