Elisp Regex Match Commas Outside of Parenthesis
I'm having an issue trying to get regex grouping to occur correctly. I have an string, say:
dtASDF[a, b, c]
I use the expression:
dt\(.*\)\[\(.*\), \(.*\), \(.*\)\]
And it groups the a
, b
, and c
seperatly, which is perfect. The issue is that the string might be:
dtASDF[a, .(b, c), .(d, e)]
In which case the above expression doesn't work, since it's just looking for commas. So the question is, since emacs doesn't have lookarounds, is there a way to group by comma seperation, but not if the commas are between parenthesis?
In other words, the grouping here should be: a
, .(b, c)
, .(d, e)
.
I'm aware similar questions exist on SO, but I couldn't find one relevant for elisp regex, which is tricky as it doesn't feature lookarounds that are typically used for this sort of thing.
regex emacs
add a comment |
I'm having an issue trying to get regex grouping to occur correctly. I have an string, say:
dtASDF[a, b, c]
I use the expression:
dt\(.*\)\[\(.*\), \(.*\), \(.*\)\]
And it groups the a
, b
, and c
seperatly, which is perfect. The issue is that the string might be:
dtASDF[a, .(b, c), .(d, e)]
In which case the above expression doesn't work, since it's just looking for commas. So the question is, since emacs doesn't have lookarounds, is there a way to group by comma seperation, but not if the commas are between parenthesis?
In other words, the grouping here should be: a
, .(b, c)
, .(d, e)
.
I'm aware similar questions exist on SO, but I couldn't find one relevant for elisp regex, which is tricky as it doesn't feature lookarounds that are typically used for this sort of thing.
regex emacs
No dice. I think you need those double escapes in emacs.
– bk18
Nov 15 '18 at 21:29
Yeah that's not doing it either. That's not matching anything. Elisp is weird about \ vs The question could probably be answered with a simpler example, in elisp, filter(asdf, asdf), asdf
into two groups:(asdf, asdf)
andasdf
– bk18
Nov 15 '18 at 21:34
In my example,[.*?]
matches just the periods indtASDF[a, .(b, c), .(d, e)]
.\[.*?\]
matches everything insde and including[...]
– bk18
Nov 15 '18 at 21:40
add a comment |
I'm having an issue trying to get regex grouping to occur correctly. I have an string, say:
dtASDF[a, b, c]
I use the expression:
dt\(.*\)\[\(.*\), \(.*\), \(.*\)\]
And it groups the a
, b
, and c
seperatly, which is perfect. The issue is that the string might be:
dtASDF[a, .(b, c), .(d, e)]
In which case the above expression doesn't work, since it's just looking for commas. So the question is, since emacs doesn't have lookarounds, is there a way to group by comma seperation, but not if the commas are between parenthesis?
In other words, the grouping here should be: a
, .(b, c)
, .(d, e)
.
I'm aware similar questions exist on SO, but I couldn't find one relevant for elisp regex, which is tricky as it doesn't feature lookarounds that are typically used for this sort of thing.
regex emacs
I'm having an issue trying to get regex grouping to occur correctly. I have an string, say:
dtASDF[a, b, c]
I use the expression:
dt\(.*\)\[\(.*\), \(.*\), \(.*\)\]
And it groups the a
, b
, and c
seperatly, which is perfect. The issue is that the string might be:
dtASDF[a, .(b, c), .(d, e)]
In which case the above expression doesn't work, since it's just looking for commas. So the question is, since emacs doesn't have lookarounds, is there a way to group by comma seperation, but not if the commas are between parenthesis?
In other words, the grouping here should be: a
, .(b, c)
, .(d, e)
.
I'm aware similar questions exist on SO, but I couldn't find one relevant for elisp regex, which is tricky as it doesn't feature lookarounds that are typically used for this sort of thing.
regex emacs
regex emacs
asked Nov 15 '18 at 21:10
bk18bk18
897317
897317
No dice. I think you need those double escapes in emacs.
– bk18
Nov 15 '18 at 21:29
Yeah that's not doing it either. That's not matching anything. Elisp is weird about \ vs The question could probably be answered with a simpler example, in elisp, filter(asdf, asdf), asdf
into two groups:(asdf, asdf)
andasdf
– bk18
Nov 15 '18 at 21:34
In my example,[.*?]
matches just the periods indtASDF[a, .(b, c), .(d, e)]
.\[.*?\]
matches everything insde and including[...]
– bk18
Nov 15 '18 at 21:40
add a comment |
No dice. I think you need those double escapes in emacs.
– bk18
Nov 15 '18 at 21:29
Yeah that's not doing it either. That's not matching anything. Elisp is weird about \ vs The question could probably be answered with a simpler example, in elisp, filter(asdf, asdf), asdf
into two groups:(asdf, asdf)
andasdf
– bk18
Nov 15 '18 at 21:34
In my example,[.*?]
matches just the periods indtASDF[a, .(b, c), .(d, e)]
.\[.*?\]
matches everything insde and including[...]
– bk18
Nov 15 '18 at 21:40
No dice. I think you need those double escapes in emacs.
– bk18
Nov 15 '18 at 21:29
No dice. I think you need those double escapes in emacs.
– bk18
Nov 15 '18 at 21:29
Yeah that's not doing it either. That's not matching anything. Elisp is weird about \ vs The question could probably be answered with a simpler example, in elisp, filter
(asdf, asdf), asdf
into two groups: (asdf, asdf)
and asdf
– bk18
Nov 15 '18 at 21:34
Yeah that's not doing it either. That's not matching anything. Elisp is weird about \ vs The question could probably be answered with a simpler example, in elisp, filter
(asdf, asdf), asdf
into two groups: (asdf, asdf)
and asdf
– bk18
Nov 15 '18 at 21:34
In my example,
[.*?]
matches just the periods in dtASDF[a, .(b, c), .(d, e)]
. \[.*?\]
matches everything insde and including [...]
– bk18
Nov 15 '18 at 21:40
In my example,
[.*?]
matches just the periods in dtASDF[a, .(b, c), .(d, e)]
. \[.*?\]
matches everything insde and including [...]
– bk18
Nov 15 '18 at 21:40
add a comment |
1 Answer
1
active
oldest
votes
How about this as the grouping pattern?
"\(\.([^)]*)\|.*?\)"
So in full:
"dt\(.*\)\[\(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\)\]"
n.b. If you want to get verbose-but-readable with the rx
macro, you can write that as:
(rx "dt" (group (zero-or-more not-newline)) "["
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
"]")
:thumbsup: Thank you. Regex will forever be a mystery to me.
– bk18
Nov 15 '18 at 21:41
I highly recommendM-x re-builder
(and see also emacs.stackexchange.com/q/5568/454 regarding that).
– phils
Nov 15 '18 at 21:42
Actually, upon further investigation, this doesn't work for everything I need. It solves my example; but what if we had:dt[a, .(b = mean(c, d), d), .(e, f)]
The second interior set of parens break it.
– bk18
Nov 15 '18 at 21:52
I'm afraid that you cannot use a regular expression to match arbitrary balanced pairs.
– phils
Nov 15 '18 at 21:56
You would instead want to write elisp code to parse it, using the likes of theforward-sexp
function to move across a balanced expression. (Which would be a different question.)
– phils
Nov 15 '18 at 21:57
add a 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%2f53327947%2felisp-regex-match-commas-outside-of-parenthesis%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
How about this as the grouping pattern?
"\(\.([^)]*)\|.*?\)"
So in full:
"dt\(.*\)\[\(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\)\]"
n.b. If you want to get verbose-but-readable with the rx
macro, you can write that as:
(rx "dt" (group (zero-or-more not-newline)) "["
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
"]")
:thumbsup: Thank you. Regex will forever be a mystery to me.
– bk18
Nov 15 '18 at 21:41
I highly recommendM-x re-builder
(and see also emacs.stackexchange.com/q/5568/454 regarding that).
– phils
Nov 15 '18 at 21:42
Actually, upon further investigation, this doesn't work for everything I need. It solves my example; but what if we had:dt[a, .(b = mean(c, d), d), .(e, f)]
The second interior set of parens break it.
– bk18
Nov 15 '18 at 21:52
I'm afraid that you cannot use a regular expression to match arbitrary balanced pairs.
– phils
Nov 15 '18 at 21:56
You would instead want to write elisp code to parse it, using the likes of theforward-sexp
function to move across a balanced expression. (Which would be a different question.)
– phils
Nov 15 '18 at 21:57
add a comment |
How about this as the grouping pattern?
"\(\.([^)]*)\|.*?\)"
So in full:
"dt\(.*\)\[\(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\)\]"
n.b. If you want to get verbose-but-readable with the rx
macro, you can write that as:
(rx "dt" (group (zero-or-more not-newline)) "["
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
"]")
:thumbsup: Thank you. Regex will forever be a mystery to me.
– bk18
Nov 15 '18 at 21:41
I highly recommendM-x re-builder
(and see also emacs.stackexchange.com/q/5568/454 regarding that).
– phils
Nov 15 '18 at 21:42
Actually, upon further investigation, this doesn't work for everything I need. It solves my example; but what if we had:dt[a, .(b = mean(c, d), d), .(e, f)]
The second interior set of parens break it.
– bk18
Nov 15 '18 at 21:52
I'm afraid that you cannot use a regular expression to match arbitrary balanced pairs.
– phils
Nov 15 '18 at 21:56
You would instead want to write elisp code to parse it, using the likes of theforward-sexp
function to move across a balanced expression. (Which would be a different question.)
– phils
Nov 15 '18 at 21:57
add a comment |
How about this as the grouping pattern?
"\(\.([^)]*)\|.*?\)"
So in full:
"dt\(.*\)\[\(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\)\]"
n.b. If you want to get verbose-but-readable with the rx
macro, you can write that as:
(rx "dt" (group (zero-or-more not-newline)) "["
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
"]")
How about this as the grouping pattern?
"\(\.([^)]*)\|.*?\)"
So in full:
"dt\(.*\)\[\(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\), \(\.([^)]*)\|.*?\)\]"
n.b. If you want to get verbose-but-readable with the rx
macro, you can write that as:
(rx "dt" (group (zero-or-more not-newline)) "["
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
", "
(group (or (seq ".(" (zero-or-more (not (any ")"))) ")")
(minimal-match (zero-or-more not-newline))))
"]")
edited Nov 15 '18 at 21:55
answered Nov 15 '18 at 21:40
philsphils
58.6k7109146
58.6k7109146
:thumbsup: Thank you. Regex will forever be a mystery to me.
– bk18
Nov 15 '18 at 21:41
I highly recommendM-x re-builder
(and see also emacs.stackexchange.com/q/5568/454 regarding that).
– phils
Nov 15 '18 at 21:42
Actually, upon further investigation, this doesn't work for everything I need. It solves my example; but what if we had:dt[a, .(b = mean(c, d), d), .(e, f)]
The second interior set of parens break it.
– bk18
Nov 15 '18 at 21:52
I'm afraid that you cannot use a regular expression to match arbitrary balanced pairs.
– phils
Nov 15 '18 at 21:56
You would instead want to write elisp code to parse it, using the likes of theforward-sexp
function to move across a balanced expression. (Which would be a different question.)
– phils
Nov 15 '18 at 21:57
add a comment |
:thumbsup: Thank you. Regex will forever be a mystery to me.
– bk18
Nov 15 '18 at 21:41
I highly recommendM-x re-builder
(and see also emacs.stackexchange.com/q/5568/454 regarding that).
– phils
Nov 15 '18 at 21:42
Actually, upon further investigation, this doesn't work for everything I need. It solves my example; but what if we had:dt[a, .(b = mean(c, d), d), .(e, f)]
The second interior set of parens break it.
– bk18
Nov 15 '18 at 21:52
I'm afraid that you cannot use a regular expression to match arbitrary balanced pairs.
– phils
Nov 15 '18 at 21:56
You would instead want to write elisp code to parse it, using the likes of theforward-sexp
function to move across a balanced expression. (Which would be a different question.)
– phils
Nov 15 '18 at 21:57
:thumbsup: Thank you. Regex will forever be a mystery to me.
– bk18
Nov 15 '18 at 21:41
:thumbsup: Thank you. Regex will forever be a mystery to me.
– bk18
Nov 15 '18 at 21:41
I highly recommend
M-x re-builder
(and see also emacs.stackexchange.com/q/5568/454 regarding that).– phils
Nov 15 '18 at 21:42
I highly recommend
M-x re-builder
(and see also emacs.stackexchange.com/q/5568/454 regarding that).– phils
Nov 15 '18 at 21:42
Actually, upon further investigation, this doesn't work for everything I need. It solves my example; but what if we had:
dt[a, .(b = mean(c, d), d), .(e, f)]
The second interior set of parens break it.– bk18
Nov 15 '18 at 21:52
Actually, upon further investigation, this doesn't work for everything I need. It solves my example; but what if we had:
dt[a, .(b = mean(c, d), d), .(e, f)]
The second interior set of parens break it.– bk18
Nov 15 '18 at 21:52
I'm afraid that you cannot use a regular expression to match arbitrary balanced pairs.
– phils
Nov 15 '18 at 21:56
I'm afraid that you cannot use a regular expression to match arbitrary balanced pairs.
– phils
Nov 15 '18 at 21:56
You would instead want to write elisp code to parse it, using the likes of the
forward-sexp
function to move across a balanced expression. (Which would be a different question.)– phils
Nov 15 '18 at 21:57
You would instead want to write elisp code to parse it, using the likes of the
forward-sexp
function to move across a balanced expression. (Which would be a different question.)– phils
Nov 15 '18 at 21:57
add a 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%2f53327947%2felisp-regex-match-commas-outside-of-parenthesis%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
No dice. I think you need those double escapes in emacs.
– bk18
Nov 15 '18 at 21:29
Yeah that's not doing it either. That's not matching anything. Elisp is weird about \ vs The question could probably be answered with a simpler example, in elisp, filter
(asdf, asdf), asdf
into two groups:(asdf, asdf)
andasdf
– bk18
Nov 15 '18 at 21:34
In my example,
[.*?]
matches just the periods indtASDF[a, .(b, c), .(d, e)]
.\[.*?\]
matches everything insde and including[...]
– bk18
Nov 15 '18 at 21:40