* Problem with regexp nested groups
@ 2008-05-10 15:59 Marc Tfardy
2008-05-10 19:32 ` Lennart Borgman (gmail)
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Marc Tfardy @ 2008-05-10 15:59 UTC (permalink / raw)
To: help-gnu-emacs
Hallo,
I have some problem with regexp and I hope someone could help me.
Assume we have following text in a buffer.
-- DATA ----------------------------------------------------------------
<DATA="some/file/sample1.mp3">
<DATA="some/file/sample2.mp3">
blablalba
<DATA="some/file/sample3.wav">
blabla
OBJ('some/file/sample4.mp3')
OBJ('some/file/sample5.au')
------------------------------------------------------------------------
My goal is to extract some text from the buffer, namely only
portion of text between `DATA="' and `"' or between `OBJ('' and
`''. In both cases text must end with `.mp3' and the left and
right delimeter shoud be ignored. So the right result should look
like this:
some/file/sample1.mp3
some/file/sample2.mp3
some/file/sample4.mp3
I wrote this test function:
(defun get-data ()
(interactive)
(if (re-search-forward
"\\(DATA=\"\\(.*?\.mp3\\)\"\\|OBJ('\\(.*?\.mp3\\)')\\)" nil t)
(message "found: %s" (match-string-no-properties 1))
(message "failed")))
With (match-string-no-properties 1) I get this result:
found: DATA="some/file/sample1.mp3"
found: DATA="some/file/sample2.mp3"
found: OBJ('some/file/sample4.mp3')
Not good. Now I replace (match-string-no-properties 1) with
(match-string-no-properties 2) and I get only two results for
both DATA lines:
found: some/file/sample1.mp3
found: some/file/sample2.mp3
found: nil
The version with (match-string-no-properties 3) returns
only OBJ line:
found: some/file/sample4.mp3
found: nil
Now the final question: how can I get results like version 2 and
3 but at ones? I would be grateful for any help
regards
Marc
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Problem with regexp nested groups
2008-05-10 15:59 Problem with regexp nested groups Marc Tfardy
@ 2008-05-10 19:32 ` Lennart Borgman (gmail)
2008-05-10 19:54 ` Phil Carmody
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Lennart Borgman (gmail) @ 2008-05-10 19:32 UTC (permalink / raw)
Cc: help-gnu-emacs
Marc Tfardy wrote:
> Hallo,
>
> I have some problem with regexp and I hope someone could help me.
>
>
> Assume we have following text in a buffer.
>
> -- DATA ----------------------------------------------------------------
> <DATA="some/file/sample1.mp3">
> <DATA="some/file/sample2.mp3">
> blablalba
> <DATA="some/file/sample3.wav">
> blabla
> OBJ('some/file/sample4.mp3')
> OBJ('some/file/sample5.au')
> ------------------------------------------------------------------------
>
> My goal is to extract some text from the buffer, namely only
> portion of text between `DATA="' and `"' or between `OBJ('' and
> `''. In both cases text must end with `.mp3' and the left and
> right delimeter shoud be ignored. So the right result should look
> like this:
>
> some/file/sample1.mp3
> some/file/sample2.mp3
> some/file/sample4.mp3
>
>
> I wrote this test function:
>
> (defun get-data ()
> (interactive)
> (if (re-search-forward
> "\\(DATA=\"\\(.*?\.mp3\\)\"\\|OBJ('\\(.*?\.mp3\\)')\\)" nil t)
> (message "found: %s" (match-string-no-properties 1))
> (message "failed")))
>
> With (match-string-no-properties 1) I get this result:
>
> found: DATA="some/file/sample1.mp3"
> found: DATA="some/file/sample2.mp3"
> found: OBJ('some/file/sample4.mp3')
>
> Not good. Now I replace (match-string-no-properties 1) with
> (match-string-no-properties 2) and I get only two results for
> both DATA lines:
>
> found: some/file/sample1.mp3
> found: some/file/sample2.mp3
> found: nil
>
> The version with (match-string-no-properties 3) returns
> only OBJ line:
>
> found: some/file/sample4.mp3
> found: nil
>
>
> Now the final question: how can I get results like version 2 and
> 3 but at ones? I would be grateful for any help
>
> regards
>
> Marc
Could you perhaps look for both (match-string-no-properties 1) and dito 2?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Problem with regexp nested groups
2008-05-10 15:59 Problem with regexp nested groups Marc Tfardy
2008-05-10 19:32 ` Lennart Borgman (gmail)
@ 2008-05-10 19:54 ` Phil Carmody
2008-05-10 22:31 ` Marc Tfardy
[not found] ` <mailman.11419.1210447961.18990.help-gnu-emacs@gnu.org>
2008-05-11 9:14 ` Johan Bockgård
3 siblings, 1 reply; 7+ messages in thread
From: Phil Carmody @ 2008-05-10 19:54 UTC (permalink / raw)
To: help-gnu-emacs
Marc Tfardy <m-t-o___CUT__IT@web.de> writes:
> Hallo,
>
> I have some problem with regexp and I hope someone could help me.
>
>
> Assume we have following text in a buffer.
>
> -- DATA ----------------------------------------------------------------
> <DATA="some/file/sample1.mp3">
> <DATA="some/file/sample2.mp3">
> blablalba
> <DATA="some/file/sample3.wav">
> blabla
> OBJ('some/file/sample4.mp3')
> OBJ('some/file/sample5.au')
> ------------------------------------------------------------------------
>
> My goal is to extract some text from the buffer, namely only
> portion of text between `DATA="' and `"' or between `OBJ('' and
> `''. In both cases text must end with `.mp3' and the left and
> right delimeter shoud be ignored.
...
> "\\(DATA=\"\\(.*?\.mp3\\)\"\\|OBJ('\\(.*?\.mp3\\)')\\)" nil t)
How about grouping /DATA="/ or /OBJ('/ as match 1, then capture
the filename as 2, and end with /.mp3"/ or /.mp3'/ as match 3?
That would match malformed lines such as
DATA="foo.mp3'
but if that's a problem you can capture the quotes too and
fix them in post-production.
Phil
--
Dear aunt, let's set so double the killer delete select all.
-- Microsoft voice recognition live demonstration
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Problem with regexp nested groups
2008-05-10 19:54 ` Phil Carmody
@ 2008-05-10 22:31 ` Marc Tfardy
0 siblings, 0 replies; 7+ messages in thread
From: Marc Tfardy @ 2008-05-10 22:31 UTC (permalink / raw)
To: help-gnu-emacs
Phil Carmody schrieb:
> Marc Tfardy <m-t-o___CUT__IT@web.de> writes:
>> Hallo,
>>
>> I have some problem with regexp and I hope someone could help me.
>>
>>
>> Assume we have following text in a buffer.
>>
>> -- DATA ----------------------------------------------------------------
>> <DATA="some/file/sample1.mp3">
>> <DATA="some/file/sample2.mp3">
>> blablalba
>> <DATA="some/file/sample3.wav">
>> blabla
>> OBJ('some/file/sample4.mp3')
>> OBJ('some/file/sample5.au')
>> ------------------------------------------------------------------------
>>
>> My goal is to extract some text from the buffer, namely only
>> portion of text between `DATA="' and `"' or between `OBJ('' and
>> `''. In both cases text must end with `.mp3' and the left and
>> right delimeter shoud be ignored.
> ...
>> "\\(DATA=\"\\(.*?\.mp3\\)\"\\|OBJ('\\(.*?\.mp3\\)')\\)" nil t)
>
> How about grouping /DATA="/ or /OBJ('/ as match 1, then capture
> the filename as 2, and end with /.mp3"/ or /.mp3'/ as match 3?
Interesting idea and it really works and it is definitely better then
multiple (match-string-no-properties x). I've modified slightly
your suggestion and I put extension togother with filename:
"\\(DATA=\"\\|OBJ('\\)\\(.*?\.mp3\\)\\(\"\\|')\\)"
So I need only to refer (match-string-no-properties 2).
> That would match malformed lines such as
>
> DATA="foo.mp3'
>
> but if that's a problem you can capture the quotes too and
> fix them in post-production.
This disadvantage I can get over. The really problem occur when
the extract part of text exist not at position 2 (in case of
another, more complicated regexp). In this case I have the same
type of problem like in my first posting.
regards
Marc
^ permalink raw reply [flat|nested] 7+ messages in thread
[parent not found: <mailman.11419.1210447961.18990.help-gnu-emacs@gnu.org>]
* Re: Problem with regexp nested groups
2008-05-10 15:59 Problem with regexp nested groups Marc Tfardy
` (2 preceding siblings ...)
[not found] ` <mailman.11419.1210447961.18990.help-gnu-emacs@gnu.org>
@ 2008-05-11 9:14 ` Johan Bockgård
2008-05-11 11:59 ` Marc Tfardy
3 siblings, 1 reply; 7+ messages in thread
From: Johan Bockgård @ 2008-05-11 9:14 UTC (permalink / raw)
To: help-gnu-emacs
Marc Tfardy <m-t-o___CUT__IT@web.de> writes:
> Not good. Now I replace (match-string-no-properties 1) with
> (match-string-no-properties 2) and I get only two results for both
> DATA lines:
>
> found: some/file/sample1.mp3
> found: some/file/sample2.mp3
> found: nil
In CVS Emacs:
"\\(DATA=\"\\(.*?\.mp3\\)\"\\|OBJ('\\(?2:.*?\.mp3\\)')\\)"
** The regexp form \(?<num>:<regexp>\) specifies the group number
explicitly.
--
Johan Bockgård
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Problem with regexp nested groups
2008-05-11 9:14 ` Johan Bockgård
@ 2008-05-11 11:59 ` Marc Tfardy
0 siblings, 0 replies; 7+ messages in thread
From: Marc Tfardy @ 2008-05-11 11:59 UTC (permalink / raw)
To: help-gnu-emacs
Johan Bockgård schrieb:
> Marc Tfardy <m-t-o___CUT__IT@web.de> writes:
>
>> Not good. Now I replace (match-string-no-properties 1) with
>> (match-string-no-properties 2) and I get only two results for both
>> DATA lines:
>>
>> found: some/file/sample1.mp3
>> found: some/file/sample2.mp3
>> found: nil
>
> In CVS Emacs:
>
> "\\(DATA=\"\\(.*?\.mp3\\)\"\\|OBJ('\\(?2:.*?\.mp3\\)')\\)"
>
> ** The regexp form \(?<num>:<regexp>\) specifies the group number
> explicitly.
Great! One reason more to install CVS version :) Thanks.
regards
Marc
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2008-05-11 11:59 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-05-10 15:59 Problem with regexp nested groups Marc Tfardy
2008-05-10 19:32 ` Lennart Borgman (gmail)
2008-05-10 19:54 ` Phil Carmody
2008-05-10 22:31 ` Marc Tfardy
[not found] ` <mailman.11419.1210447961.18990.help-gnu-emacs@gnu.org>
2008-05-10 22:09 ` Marc Tfardy
2008-05-11 9:14 ` Johan Bockgård
2008-05-11 11:59 ` Marc Tfardy
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.