Regexp Functions
If the string is "A ... B ... " , using Regexp Functions is it possible, if 'A' matches [-] then 'B' would be replaced by "C"?'A' is first in the string. Position of B is not constant.and may not be there. Thank you, ƒg
Re: Regexp Functions
Hello! Em seg., 8 de jun. de 2020 às 08:37, Freeman Gilmore < freeman.gilm...@gmail.com> escreveu: > If the string is "A ... B ... " , using Regexp Functions is it > possible, if 'A' matches [-] then 'B' would be replaced by "C"?'A' > is first in the string. Position of B is not constant.and may not be > there. > Can you provide a little more context of what you are trying to do and what this has to to with lilypond? What tools are you using to find and replace strings using regex? Anyway, here is a solution of what I _think_ is your problem using 'sed' in a Linux terminal. Suppose we have a file regex_test.txt with the following content: - I want to replace [B] for [C] - But don't want to replace anything here This line has [B], but doesn't start with the dash. So nothing will be replaced here. --- The following command should replace the first [B] with [C]: sed '/^-.*\[B\]/s/\[B\]/[C]/' regex_test.txt output: - I want to replace [C] for [C] - But don't want to replace anything here This line has [B], but doesn't start with the dash. So nothing will be replaced here. If you are happy with the result, you can write direclty to the file using the -i flag sed -i '/^-.*\[B\]/s/\[B\]/[C]/' regex_test.txt Explanation: We provide to sed a patern with four parts separated by a slash '/'. The first part ^-.*\[B\] means a line starting (^) with a dash followed by anything (.*) and containing a [B] in it. We had to prefix the brackets [ and ] with backslashes to tell sed these are literally the caracters [ and ], because these are special characters in regex. The other parts s/\[B\]/[C] mean substitute (the command s) the element [B] (notice the backslashes here again) with the element [C] (no backslashes needed here). Finally, of course, we pass to sed the file to do the operation. Hope that helps! Caio
Re: Regexp Functions
On 2020-06-08 12:47 pm, Caio Barros wrote: Hello! Em seg., 8 de jun. de 2020 às 08:37, Freeman Gilmore < freeman.gilm...@gmail.com> escreveu: If the string is "A ... B ... " , using Regexp Functions is it possible, if 'A' matches [-] then 'B' would be replaced by "C"?'A' is first in the string. Position of B is not constant.and may not be there. Can you provide a little more context of what you are trying to do and what this has to to with lilypond? What tools are you using to find and replace strings using regex? LilyPond -> Guile -> (use-modules (ice-9 regex)) Or I could be making the wrong assumption here. ¯\_(ツ)_/¯ (display (regexp-substitute/global #f (make-regexp "(^A.*)B" regexp/newline) (string-join '("A ... B ..." "D ... B ..." "A ... E ..." "A ... B ...") "\n" 'suffix) 'pre 1 "C" 'post)) A ... C ... D ... B ... A ... E ... A ... C ... The key here is to use a capturing group for the portion of the string before "B" that you need to preserve in the substitution. -- Aaron Hill
Re: Regexp Functions
Caio and Arron: I am writing this at the top to start over and answer Caio's questions. Say you have an accidental string name "A..." and the stem is up; then the same accidental with the stem down would be named "-A...". "A".. would be a short string like LilyPond uses for accidentals. If you want to use the accidental more than one time say 2 then write the strings like this, "A...x2" and "-A...x2". But the second name is wrong, the "x2" part needs to go with the stem."A...x2" is the logical entry but the "x" needs to be replaced with "-x". Before I do this I need a space before the "x", i replace "x",, if it exists with " x" if it exists. I now have the 4 possibilities "A...", "-A...", "A... x2", "-A... x2". My question is, is there a procedure that sees the [-] then searches for "x" and replaces it with "-x". I could add the "-" to the "x" at entry but want to keep it simple, x (times ) 2 is logical (and saves a stroke). The next step would be to split the strings into two strings if a space exists.So "A... x2" becomes "A..." " x2" and "-A... x2" becomes "-A..." "-x2" ; two glyph each to form ligatures. Thank you, ƒg On Mon, Jun 8, 2020 at 4:31 PM Aaron Hill wrote: > On 2020-06-08 12:47 pm, Caio Barros wrote: > > Hello! > > > > Em seg., 8 de jun. de 2020 às 08:37, Freeman Gilmore < > > freeman.gilm...@gmail.com> escreveu: > > > >> If the string is "A ... B ... " , using Regexp Functions is it > >> possible, if 'A' matches [-] then 'B' would be replaced by "C"?'A' > >> is first in the string. Position of B is not constant.and may not be > >> there. > >> > > > > Can you provide a little more context of what you are trying to do and > > what > > this has to to with lilypond? What tools are you using to find and > > replace > > strings using regex? > > LilyPond -> Guile -> (use-modules (ice-9 regex)) > > Or I could be making the wrong assumption here. ¯\_(ツ)_/¯ > > > (display >(regexp-substitute/global #f > (make-regexp "(^A.*)B" regexp/newline) > (string-join '("A ... B ..." "D ... B ..." > "A ... E ..." "A ... B ...") > "\n" 'suffix) > 'pre 1 "C" 'post)) > > > > A ... C ... > D ... B ... > A ... E ... > A ... C ... > > > The key here is to use a capturing group for the portion of the string > before "B" that you need to preserve in the substitution. > > > -- Aaron Hill > >
Re: Regexp Functions
Hi! I find your description difficult to understand. Maybe you could provide an set of examples showing exactly what you wish to be modified into what. Apart from that your problem sounds as if it is trivial when using non greedy regular expressions. As Aaron already wrote you need expressions to capture the whole block and isolate the subexpressions you wish to operate on (A, ... and x in your case - if I understand you correctly) Kind regards, Michael Mobil gesendet > Am 09.06.2020 um 07:00 schrieb Freeman Gilmore : > > > Caio and Arron: > > I am writing this at the top to start over and answer Caio's questions. > Say you have an accidental string name "A..." and the stem is up; then the > same accidental with the stem down would be named "-A...". "A".. would > be a short string like LilyPond uses for accidentals. > > If you want to use the accidental more than one time say 2 then write the > strings like this, "A...x2" and "-A...x2". But the second name is wrong, > the "x2" part needs to go with the stem."A...x2" is the logical entry but > the "x" needs to be replaced with "-x". Before I do this I need a space > before the "x", i replace "x",, if it exists with " x" if it exists. I > now have the 4 possibilities "A...", "-A...", "A... x2", "-A... x2". > > My question is, is there a procedure that sees the [-] then searches for "x" > and replaces it with "-x". > > I could add the "-" to the "x" at entry but want to keep it simple, x (times > ) 2 is logical (and saves a stroke). The next step would be to split the > strings into two strings if a space exists.So "A... x2" becomes "A..." > " x2" and "-A... x2" becomes "-A..." "-x2" ; two glyph each to form > ligatures. > > Thank you, ƒg > >> On Mon, Jun 8, 2020 at 4:31 PM Aaron Hill wrote: >> On 2020-06-08 12:47 pm, Caio Barros wrote: >> > Hello! >> > >> > Em seg., 8 de jun. de 2020 às 08:37, Freeman Gilmore < >> > freeman.gilm...@gmail.com> escreveu: >> > >> >> If the string is "A ... B ... " , using Regexp Functions is it >> >> possible, if 'A' matches [-] then 'B' would be replaced by "C"?'A' >> >> is first in the string. Position of B is not constant.and may not be >> >> there. >> >> >> > >> > Can you provide a little more context of what you are trying to do and >> > what >> > this has to to with lilypond? What tools are you using to find and >> > replace >> > strings using regex? >> >> LilyPond -> Guile -> (use-modules (ice-9 regex)) >> >> Or I could be making the wrong assumption here. ¯\_(ツ)_/¯ >> >> >> (display >>(regexp-substitute/global #f >> (make-regexp "(^A.*)B" regexp/newline) >> (string-join '("A ... B ..." "D ... B ..." >> "A ... E ..." "A ... B ...") >> "\n" 'suffix) >> 'pre 1 "C" 'post)) >> >> >> >> A ... C ... >> D ... B ... >> A ... E ... >> A ... C ... >> >> >> The key here is to use a capturing group for the portion of the string >> before "B" that you need to preserve in the substitution. >> >> >> -- Aaron Hill >>
Re: Regexp Functions
On Mon, Jun 8, 2020 at 4:31 PM Aaron Hill wrote: > > On 2020-06-08 12:47 pm, Caio Barros wrote: > > Hello! > > > > Em seg., 8 de jun. de 2020 às 08:37, Freeman Gilmore < > > freeman.gilm...@gmail.com> escreveu: > > > >> If the string is "A ... B ... " , using Regexp Functions is it > >> possible, if 'A' matches [-] then 'B' would be replaced by "C"?'A' > >> is first in the string. Position of B is not constant.and may not be > >> there. > >> > > > > Can you provide a little more context of what you are trying to do and > > what > > this has to to with lilypond? What tools are you using to find and > > replace > > strings using regex? > > LilyPond -> Guile -> (use-modules (ice-9 regex)) > > Or I could be making the wrong assumption here. ¯\_(ツ)_/¯ > > > (display >(regexp-substitute/global #f > (make-regexp "(^A.*)B" regexp/newline) > (string-join '("A ... B ..." "D ... B ..." > "A ... E ..." "A ... B ...") > "\n" 'suffix) > 'pre 1 "C" 'post)) > > > > A ... C ... > D ... B ... > A ... E ... > A ... C ... > > > The key here is to use a capturing group for the portion of the string > before "B" that you need to preserve in the substitution. I do no tthink this is what i want. Let me try again Say you have "Xsdfghjkl" If "x" is the first character then replace the "g" if it exist with "Y" => "XsdYfhjkl"X Thank you,ƒg > > > -- Aaron Hill >
Re: Regexp Functions
On Tue, Jun 9, 2020 at 1:25 AM Michael Gerdau wrote: > > Hi! > > I find your description difficult to understand. Maybe you could provide an > set of examples showing exactly what you wish to be modified into what. Sorry, that is the problem I have when someone asks about what i am doing or trying to do. For me I find it best to just ask what I want to know. Arron as well as some others answer my question when I do not include these kinds of detail they do add confusion. Arron on my initial post did not understand what I was asking eathere. So I will try again.Say you have "Xsdfghjkl" If "x" is the first character then replace the "g" if it exist with "Y" => "XsdYfhjkl"X > Apart from that your problem sounds as if it is trivial when using non greedy > regular expressions. I do not know what this means: non greedy regular expressions? Thank you, ƒg As Aaron already wrote you need expressions to capture the whole block and isolate the subexpressions you wish to operate on (A, ... and x in your case - if I understand you correctly) > > Kind regards, > Michael > > Mobil gesendet > > Am 09.06.2020 um 07:00 schrieb Freeman Gilmore : > > > Caio and Arron: > > I am writing this at the top to start over and answer Caio's questions. > Say you have an accidental string name "A..." and the stem is up; then the > same accidental with the stem down would be named "-A...". "A".. would > be a short string like LilyPond uses for accidentals. > > If you want to use the accidental more than one time say 2 then write the > strings like this, "A...x2" and "-A...x2". But the second name is wrong, > the "x2" part needs to go with the stem."A...x2" is the logical entry but > the "x" needs to be replaced with "-x". Before I do this I need a space > before the "x", i replace "x",, if it exists with " x" if it exists. I > now have the 4 possibilities "A...", "-A...", "A... x2", "-A... x2". > > My question is, is there a procedure that sees the [-] then searches for "x" > and replaces it with "-x". > > I could add the "-" to the "x" at entry but want to keep it simple, x (times > ) 2 is logical (and saves a stroke). The next step would be to split the > strings into two strings if a space exists.So "A... x2" becomes "A..." > " x2" and "-A... x2" becomes "-A..." "-x2" ; two glyph each to form > ligatures. > > Thank you, ƒg > > On Mon, Jun 8, 2020 at 4:31 PM Aaron Hill wrote: >> >> On 2020-06-08 12:47 pm, Caio Barros wrote: >> > Hello! >> > >> > Em seg., 8 de jun. de 2020 às 08:37, Freeman Gilmore < >> > freeman.gilm...@gmail.com> escreveu: >> > >> >> If the string is "A ... B ... " , using Regexp Functions is it >> >> possible, if 'A' matches [-] then 'B' would be replaced by "C"?'A' >> >> is first in the string. Position of B is not constant.and may not be >> >> there. >> >> >> > >> > Can you provide a little more context of what you are trying to do and >> > what >> > this has to to with lilypond? What tools are you using to find and >> > replace >> > strings using regex? >> >> LilyPond -> Guile -> (use-modules (ice-9 regex)) >> >> Or I could be making the wrong assumption here. ¯\_(ツ)_/¯ >> >> >> (display >>(regexp-substitute/global #f >> (make-regexp "(^A.*)B" regexp/newline) >> (string-join '("A ... B ..." "D ... B ..." >> "A ... E ..." "A ... B ...") >> "\n" 'suffix) >> 'pre 1 "C" 'post)) >> >> >> >> A ... C ... >> D ... B ... >> A ... E ... >> A ... C ... >> >> >> The key here is to use a capturing group for the portion of the string >> before "B" that you need to preserve in the substitution. >> >> >> -- Aaron Hill >>
Re: Regexp Functions
Hello Freeman, > Le 9 juin 2020 à 09:43, Freeman Gilmore a écrit : > > On Tue, Jun 9, 2020 at 1:25 AM Michael Gerdau wrote: >> >> Hi! >> >> I find your description difficult to understand. Maybe you could provide an >> set of examples showing exactly what you wish to be modified into what. > Sorry, that is the problem I have when someone asks about what i am > doing or trying to do. For me I find it best to just ask what I want > to know. Arron as well as some others answer my question when I do > not include these kinds of detail they do add confusion. Arron on my > initial post did not understand what I was asking eathere. > > So I will try again.Say you have "Xsdfghjkl" If "x" is the first > character then replace the "g" if it exist with "Y" => "XsdYfhjkl"X > >> Apart from that your problem sounds as if it is trivial when using non >> greedy regular expressions. > > I do not know what this means: non greedy regular expressions? Greedy relates to how many characters are consumed by regular expressions when a match if found. You may need to learn more deeply about these to reach your goal. JM
Re: Regexp Functions
On Tue, Jun 9, 2020 at 6:51 AM Jacques Menu wrote: > > Hello Freeman, > > > Le 9 juin 2020 à 09:43, Freeman Gilmore a écrit > > : > > > > On Tue, Jun 9, 2020 at 1:25 AM Michael Gerdau wrote: > >> > >> Hi! > >> > >> I find your description difficult to understand. Maybe you could provide > >> an set of examples showing exactly what you wish to be modified into what. > > Sorry, that is the problem I have when someone asks about what i am > > doing or trying to do. For me I find it best to just ask what I want > > to know. Arron as well as some others answer my question when I do > > not include these kinds of detail they do add confusion. Arron on my > > initial post did not understand what I was asking eathere. > > > > So I will try again.Say you have "Xsdfghjkl" If "x" is the first > > character then replace the "g" if it exist with "Y" => "XsdYfhjkl"X > > > >> Apart from that your problem sounds as if it is trivial when using non > >> greedy regular expressions. > > > > I do not know what this means: non greedy regular expressions? > > Greedy relates to how many characters are consumed by regular expressions > when a match if found. > > You may need to learn more deeply about these to reach your goal. That definition helps. I use the Guile manual, the section Regular Expressions is miss leading about what is there.I only went there because Arron Hill told me about it And the introduction states "A full description of regular expressions and their syntax is beyond the scope of this manual"; and the URL goes nowhere. And I would guess knowing what to read elsewhere that is consistent with the guile version Lilypond uses may be a probleblen. I am open to a good ron cryptic reference? Thank you, ƒg > > JM > >
Re: Regexp Functions
Hello Freeman, The reference I use is http://www.cplusplus.com/reference/regex/ECMAScript/ . Greed is mentioned under ‘Quantifiers‘ HTH! JM > Le 9 juin 2020 à 15:55, Freeman Gilmore a écrit : > > On Tue, Jun 9, 2020 at 6:51 AM Jacques Menu wrote: >> >> Hello Freeman, >> >>> Le 9 juin 2020 à 09:43, Freeman Gilmore a écrit >>> : >>> >>> On Tue, Jun 9, 2020 at 1:25 AM Michael Gerdau wrote: Hi! I find your description difficult to understand. Maybe you could provide an set of examples showing exactly what you wish to be modified into what. >>> Sorry, that is the problem I have when someone asks about what i am >>> doing or trying to do. For me I find it best to just ask what I want >>> to know. Arron as well as some others answer my question when I do >>> not include these kinds of detail they do add confusion. Arron on my >>> initial post did not understand what I was asking eathere. >>> >>> So I will try again.Say you have "Xsdfghjkl" If "x" is the first >>> character then replace the "g" if it exist with "Y" => "XsdYfhjkl"X >>> Apart from that your problem sounds as if it is trivial when using non greedy regular expressions. >>> >>> I do not know what this means: non greedy regular expressions? >> >> Greedy relates to how many characters are consumed by regular expressions >> when a match if found. >> >> You may need to learn more deeply about these to reach your goal. > > That definition helps. I use the Guile manual, the section Regular > Expressions is miss leading about what is there.I only went there > because Arron Hill told me about it And the introduction states "A > full description of regular expressions and their syntax is beyond the > scope of this manual"; and the URL goes nowhere. And I would guess > knowing what to read elsewhere that is consistent with the guile > version Lilypond uses may be a probleblen. I am open to a good ron > cryptic reference? > > Thank you, ƒg > >> >> JM
Re: Regexp Functions
I've always found http://www.regular-expressions.info/ very helpful. -- Phil Holmes - Original Message - From: "Freeman Gilmore" To: "Jacques Menu" Cc: "Lilypond-User Mailing List" Sent: Tuesday, June 09, 2020 2:55 PM Subject: Re: Regexp Functions On Tue, Jun 9, 2020 at 6:51 AM Jacques Menu wrote: Hello Freeman, > Le 9 juin 2020 à 09:43, Freeman Gilmore a > écrit : > > On Tue, Jun 9, 2020 at 1:25 AM Michael Gerdau wrote: >> >> Hi! >> >> I find your description difficult to understand. Maybe you could >> provide an set of examples showing exactly what you wish to be modified >> into what. > Sorry, that is the problem I have when someone asks about what i am > doing or trying to do. For me I find it best to just ask what I want > to know. Arron as well as some others answer my question when I do > not include these kinds of detail they do add confusion. Arron on my > initial post did not understand what I was asking eathere. > > So I will try again.Say you have "Xsdfghjkl" If "x" is the first > character then replace the "g" if it exist with "Y" => "XsdYfhjkl"X > >> Apart from that your problem sounds as if it is trivial when using non >> greedy regular expressions. > > I do not know what this means: non greedy regular expressions? Greedy relates to how many characters are consumed by regular expressions when a match if found. You may need to learn more deeply about these to reach your goal. That definition helps. I use the Guile manual, the section Regular Expressions is miss leading about what is there.I only went there because Arron Hill told me about it And the introduction states "A full description of regular expressions and their syntax is beyond the scope of this manual"; and the URL goes nowhere. And I would guess knowing what to read elsewhere that is consistent with the guile version Lilypond uses may be a probleblen. I am open to a good ron cryptic reference? Thank you, ƒg JM
Re: Regexp Functions
Thank you that should help, ƒg On Tue, Jun 9, 2020 at 10:01 AM Jacques Menu wrote: > > Hello Freeman, > > The reference I use is http://www.cplusplus.com/reference/regex/ECMAScript/ . > > Greed is mentioned under ‘Quantifiers‘ > > HTH! > > JM > > > Le 9 juin 2020 à 15:55, Freeman Gilmore a écrit > > : > > > > On Tue, Jun 9, 2020 at 6:51 AM Jacques Menu wrote: > >> > >> Hello Freeman, > >> > >>> Le 9 juin 2020 à 09:43, Freeman Gilmore a > >>> écrit : > >>> > >>> On Tue, Jun 9, 2020 at 1:25 AM Michael Gerdau wrote: > > Hi! > > I find your description difficult to understand. Maybe you could provide > an set of examples showing exactly what you wish to be modified into > what. > >>> Sorry, that is the problem I have when someone asks about what i am > >>> doing or trying to do. For me I find it best to just ask what I want > >>> to know. Arron as well as some others answer my question when I do > >>> not include these kinds of detail they do add confusion. Arron on my > >>> initial post did not understand what I was asking eathere. > >>> > >>> So I will try again.Say you have "Xsdfghjkl" If "x" is the first > >>> character then replace the "g" if it exist with "Y" => "XsdYfhjkl"X > >>> > Apart from that your problem sounds as if it is trivial when using non > greedy regular expressions. > >>> > >>> I do not know what this means: non greedy regular expressions? > >> > >> Greedy relates to how many characters are consumed by regular expressions > >> when a match if found. > >> > >> You may need to learn more deeply about these to reach your goal. > > > > That definition helps. I use the Guile manual, the section Regular > > Expressions is miss leading about what is there.I only went there > > because Arron Hill told me about it And the introduction states "A > > full description of regular expressions and their syntax is beyond the > > scope of this manual"; and the URL goes nowhere. And I would guess > > knowing what to read elsewhere that is consistent with the guile > > version Lilypond uses may be a probleblen. I am open to a good ron > > cryptic reference? > > > > Thank you, ƒg > > > >> > >> JM >
Re: Regexp Functions
That looks good Thank you, ƒg On Tue, Jun 9, 2020 at 10:26 AM Phil Holmes wrote: > > I've always found http://www.regular-expressions.info/ very helpful. > > -- > Phil Holmes > > > - Original Message - > From: "Freeman Gilmore" > To: "Jacques Menu" > Cc: "Lilypond-User Mailing List" > Sent: Tuesday, June 09, 2020 2:55 PM > Subject: Re: Regexp Functions > > > On Tue, Jun 9, 2020 at 6:51 AM Jacques Menu wrote: > > > > Hello Freeman, > > > > > Le 9 juin 2020 à 09:43, Freeman Gilmore a > > > écrit : > > > > > > On Tue, Jun 9, 2020 at 1:25 AM Michael Gerdau wrote: > > >> > > >> Hi! > > >> > > >> I find your description difficult to understand. Maybe you could > > >> provide an set of examples showing exactly what you wish to be modified > > >> into what. > > > Sorry, that is the problem I have when someone asks about what i am > > > doing or trying to do. For me I find it best to just ask what I want > > > to know. Arron as well as some others answer my question when I do > > > not include these kinds of detail they do add confusion. Arron on my > > > initial post did not understand what I was asking eathere. > > > > > > So I will try again.Say you have "Xsdfghjkl" If "x" is the first > > > character then replace the "g" if it exist with "Y" => "XsdYfhjkl"X > > > > > >> Apart from that your problem sounds as if it is trivial when using non > > >> greedy regular expressions. > > > > > > I do not know what this means: non greedy regular expressions? > > > > Greedy relates to how many characters are consumed by regular expressions > > when a match if found. > > > > You may need to learn more deeply about these to reach your goal. > > That definition helps. I use the Guile manual, the section Regular > Expressions is miss leading about what is there.I only went there > because Arron Hill told me about it And the introduction states "A > full description of regular expressions and their syntax is beyond the > scope of this manual"; and the URL goes nowhere. And I would guess > knowing what to read elsewhere that is consistent with the guile > version Lilypond uses may be a probleblen. I am open to a good ron > cryptic reference? > > Thank you, ƒg > > > > > JM > > > > > >
Re: Regexp Functions
[sent this earlier and forgot to include the list...] > That definition helps. I use the Guile manual, the section Regular > Expressions is miss leading about what is there.I only went there > because Arron Hill told me about it And the introduction states "A > full description of regular expressions and their syntax is beyond the > scope of this manual"; and the URL goes nowhere. And I would guess > knowing what to read elsewhere that is consistent with the guile > version Lilypond uses may be a probleblen. I am open to a good ron > cryptic reference? I personally really like the perlre manpage. It is THE reference for all Perl Regular Expressions from which many other current regexp engines are derived. Kind regards, Michael -- Michael Gerdau email: m...@qata.de GPG-keys available on request or at public keyserver
Re: Regexp Functions
On 2020-06-09 9:25 am, Michael Gerdau wrote: [sent this earlier and forgot to include the list...] That definition helps. I use the Guile manual, the section Regular Expressions is miss leading about what is there.I only went there because Arron Hill told me about it And the introduction states "A full description of regular expressions and their syntax is beyond the scope of this manual"; and the URL goes nowhere. And I would guess knowing what to read elsewhere that is consistent with the guile version Lilypond uses may be a probleblen. I am open to a good ron cryptic reference? I personally really like the perlre manpage. It is THE reference for all Perl Regular Expressions from which many other current regexp engines are derived. Just be aware that Guile uses GNU's version of POSIX extended regular expressions (ERE), which is significantly more limited than Perl-compatible regular expressions (PCRE). GNU adds some features beyond the core POSIX standard, such as shorthands for word characters (\w) and whitespace (\s). But it omits the one for digits (\d) that is included in PCRE. Instead, one must use either /[0-9]/ or /[[:digit:]]/, the latter being an example of a POSIX character class. GNU ERE includes support for the {,n} quantifier that matches an item between zero and n times; however, quantifiers in POSIX ERE are always greedy, whereas PCRE supports lazy and possessive variants. POSIX ERE permits only basic unnamed capturing groups, although GNU ERE does add support for backreferences: /.(.)(.)\2\1/ will match 'xyzzy'. However, PCRE additionally handles non-capturing groups, named groups, and many forms of lookarounds and conditionals. -- Aaron Hill
Re: Regexp Functions
On 2020-06-09 12:42 am, Freeman Gilmore wrote: I do no tthink this is what i want. Let me try again Say you have "Xsdfghjkl" If "x" is the first character then replace the "g" if it exist with "Y" => "XsdYfhjkl"X /(^A.*)B/ is the general pattern: () Match the regex below and capture its match into backreference number 1. ^ Assert position at the beginning of a line. A Match the character "A" literally. .*Match any single character that is NOT a line break character between zero and unlimited times, as many times as possible, giving back as needed (greedy). B Match the character "B" literally. Since "A" and "B" above are literals, they may be replaced with "X" and "g", respectively, if that is what you wanted. Consider: (regexp-substitute/global #f "(^X.*)g" "Xsdfghjkl" 'pre 1 "Y" 'post) "XsdfYhjkl" Note that regular expressions can be a powerful tool [1], but they can also create more problems than they solve [2]. [1]: https://xkcd.com/208/ [2]: https://xkcd.com/1171/ Your original problem involved conditionally replacing a substring based on whether the string starts with a particular prefix. Consider: ((lambda (s) (if (string-prefix? "X" s) (string-join (string-split s #\g) "Y") s)) "Xsdfghjkl") "XsdfYhjkl" In the above, we have separated the task into a few parts. First is checking the prefix of the string, as the absence of the desired text means no work needs to be done. When replacing, we use string-split and string-join to achieve our goal. This works because we are looking for a single character to replace. A more general approach would need to use several of the string-* family of procedures: (define (string-find-replace s1 s2 s3) "Return the string @var{s1}, where all occurrences of @var{s2} are replaced by @var{s3}." (let ((index (string-contains s1 s2))) (if (number? index) (string-append (string-take s1 index) s3 (string-find-replace (string-drop s1 (+ index (string-length s2))) s2 s3)) s1))) ((lambda (s) (if (string-prefix? "XX" s) (string-find-replace s "gg" "YY") s)) "XXssddffgghhjjkkll") "XXssddffYYhhjjkkll" Hopefully you can see that in this situation, regexp-substitute/global becomes the more succinct way to express things: (regexp-substitute/global #f "(^XX.*)gg" "XXssddffgghhjjkkll" 'pre 1 "YY" 'post) "XXssddffYYhhjjkkll" -- Aaron Hill
Re: Regexp Functions
Thank you Aaron. : In order to ask my question, not knowing how to ask, I simplified it too much. The one or both of the first two below may work but i do not know how to apply them. Say I have "-y -ax3 +rx2 -stx2 t"I wanted, if "-" followed "x" before a space, then replace the "x" with " -x" for each.If I use "(^-.*)x" "-y -ax3 +rx2 -stx2 t" 'pre 1 " -x" 'post) , from what I have read, this would happen at the first "x" from the right. So that will not work. My next step was to convert the string to a list of strings. So if i convert first, "-y -ax3 +rx2 -stx2 t" => ("-y" "-ax3" "+rx2" "-stx2" "t") . I would guess that one or both of the first two below could be applied to the list of strings.But i do not have a clue? Starting with "-y -ax3 +rx2 -stx2 t" , ending with ("-y" "-a" "-x3" "+rx2" "-st" "-x2" "t") Thank you, ƒg On Tue, Jun 9, 20'20 at 5:40 PM Aaron Hill wrote: > > On 2020-06-09 12:42 am, Freeman Gilmore wrote: > > I do no tthink this is what i want. Let me try again Say you have > > "Xsdfghjkl" If "x" is the first > > character then replace the "g" if it exist with "Y" => > > "XsdYfhjkl"X > > /(^A.*)B/ is the general pattern: > > () Match the regex below and capture its match into backreference > number 1. >^ Assert position at the beginning of a line. > A Match the character "A" literally. > .*Match any single character that is NOT a line break character > between zero and unlimited times, as many times as possible, > giving back as needed (greedy). > B Match the character "B" literally. > > Since "A" and "B" above are literals, they may be replaced with "X" and > "g", respectively, if that is what you wanted. Consider: > > > (regexp-substitute/global #f >"(^X.*)g" >"Xsdfghjkl" >'pre 1 "Y" 'post) > > > > "XsdfYhjkl" > > > Note that regular expressions can be a powerful tool [1], but they can > also create more problems than they solve [2]. > > [1]: https://xkcd.com/208/ > [2]: https://xkcd.com/1171/ > > Your original problem involved conditionally replacing a substring based > on whether the string starts with a particular prefix. Consider: > > > ((lambda (s) > (if (string-prefix? "X" s) >(string-join (string-split s #\g) "Y") >s)) > "Xsdfghjkl") > > > > "XsdfYhjkl" > > > In the above, we have separated the task into a few parts. First is > checking the prefix of the string, as the absence of the desired text > means no work needs to be done. When replacing, we use string-split and > string-join to achieve our goal. This works because we are looking for > a single character to replace. > > A more general approach would need to use several of the string-* family > of procedures: > > > (define (string-find-replace s1 s2 s3) >"Return the string @var{s1}, where all occurrences > of @var{s2} are replaced by @var{s3}." >(let ((index (string-contains s1 s2))) > (if (number? index) >(string-append > (string-take s1 index) > s3 > (string-find-replace >(string-drop s1 (+ index (string-length s2))) >s2 >s3)) >s1))) > ((lambda (s) > (if (string-prefix? "XX" s) >(string-find-replace s "gg" "YY") >s)) > "XXssddffgghhjjkkll") > > > > "XXssddffYYhhjjkkll" > > > Hopefully you can see that in this situation, regexp-substitute/global > becomes the more succinct way to express things: > > > (regexp-substitute/global #f >"(^XX.*)gg" >"XXssddffgghhjjkkll" >'pre 1 "YY" 'post) > > > > "XXssddffYYhhjjkkll" > > > > -- Aaron Hill
Re: Regexp Functions
On 2020-06-15 5:57 pm, Freeman Gilmore wrote: Thank you Aaron. : In order to ask my question, not knowing how to ask, I simplified it too much. The one or both of the first two below may work but i do not know how to apply them. This is a meta problem I have often struggled with. While trying to solve problem A on my own, I encounter problem B of which I need help. So I seek guidance on problem B while omitting the context of problem A. The answers found might address problem B but do not work well in the greater context. The philosophical question is whether I should have originally framed my question about problem B with the scope of problem A or if I should have simply asked about problem A and allow the natural discussion to bring up problem B on its own. Seeing more of what you have in mind below, I am confused about its connection with LilyPond. On the one hand, I think I can help you wrangle your text in the way you want; but I am worried that we are fighting a bit of the meta problem above. What is this text intended to represent with regards to music? What are the semantics of the transformation you want to apply? Could there be another way to express things that might be easier to work with or that might better fit the patterns of LilyPond? Mind you, the answers to those questions would be taking us from the narrow problem into the broader scope. And perhaps only you truly benefit from answering them. Say I have "-y -ax3 +rx2 -stx2 t"I wanted, if "-" followed "x" before a space, then replace the "x" with " -x" for each.If I use "(^-.*)x" "-y -ax3 +rx2 -stx2 t" 'pre 1 " -x" 'post) , from what I have read, this would happen at the first "x" from the right. So that will not work. Ah, there are at least two critical pieces of information I overlooked. The prefix in question does not necessarily occur at the beginning of the string. Also, the string we are looking for might occur multiple times so we want the nearest match. We can address this as follows by being more precise in our regular expression. /^/ becomes /(^|\s)/ Here we are using alteration to assert that we are either at the beginning of the string or that we have just seen a whitespace character. /.*/ becomes /[^x]*/ All quantifiers in POSIX ERE (and thusly GNU ERE) are greedy, so care must be taken to limit what can be matched so we do not grab too much. We do not want an "x" to be matched, so we use a negated character class. Let us see this working: (regexp-substitute/global #f "((^|\\s)-[^x]*)(x)" "-y -ax3 +rx2 -stx2 t" 'pre 1 " -" 3 'post) "-y -a -x3 +rx2 -st -x2 t" Seems good. But therein lies the problem with test-driven development (TDD). Our code will only be as good as our test cases, which is why we are instructed to "test everything that can possibly break". There is a hidden problem above due to using /[^x]*/. This matches more than what we want. In particular, it matches whitespace. The first substitution in the above example was not effectively operating on "-ax3" but rather on "-y -ax3". The end result looks correct, but what if we had said "-y +ax3"? This would have been matched because we included whitespace, resulting in "-y +a -x3", which is presumably incorrect. The fix is simple: add [:space:] to the negated character class. Below I have amended the test string to include "-z +bx4" which should remain unchanged. (regexp-substitute/global #f "((^|\\s)-[^x[:space:]]*)(x)" "-y -ax3 +rx2 -stx2 t -z +bx4" 'pre 1 " -" 3 'post) "-y -a -x3 +rx2 -st -x2 t -z +bx4" What about "-x"? Our expression will match that because we are using the zero-or-more quantifier (*). The resulting substitution, "- -x", is probably not what we want. We should ensure that there is at least one non-"x" character: (regexp-substitute/global #f "((^|\\s)-[^x[:space:]]+)(x)" "-y -ax3 +rx2 -stx2 t -z +bx4 -x" 'pre 1 " -" 3 'post) "-y -a -x3 +rx2 -st -x2 t -z +bx4 -x" My next step was to convert the string to a list of strings. So if i convert first, "-y -ax3 +rx2 -stx2 t" => ("-y" "-ax3" "+rx2" "-stx2" "t") . I would guess that one or both of the first two below could be applied to the list of strings.But i do not have a clue? Starting with "-y -ax3 +rx2 -stx2 t" , ending with ("-y" "-a" "-x3" "+rx2" "-st" "-x2" "t") This approach could be made to work as well. Having split the original string by whitespace, we no longer have to worry about its impact in our expression. We trade off having a simpler regular expression while requiring more logic outside. (map (lambda (str) (regexp-substitute/global #f "(^-[^x]+)(x)" str 'pre 1 " -" 2 'post)) (string-split "-y -ax3 +rx2 -stx2 t -z +bx4 -x" #\sp)) ("-y" "-a -x3" "+rx2" "-st -x2" "t" "-z" "+bx4" "-x") Close. Where we go from here depends on what you actually
Re: Regexp Functions
On Mon, Jun 15, 2020 at 10:32 PM Aaron Hill wrote: > > On 2020-06-15 5:57 pm, Freeman Gilmore wrote: > > Thank you Aaron. : > > > > In order to ask my question, not knowing how to ask, I simplified it > > too much. The one or both of the first two below may work but i do > > not know how to apply them. > > This is a meta problem I have often struggled with. While trying to > solve problem A on my own, I encounter problem B of which I need help. > So I seek guidance on problem B while omitting the context of problem A. > The answers found might address problem B but do not work well in the > greater context. The philosophical question is whether I should have > originally framed my question about problem B with the scope of problem > A or if I should have simply asked about problem A and allow the natural > discussion to bring up problem B on its own. > > Seeing more of what you have in mind below, I am confused about its > connection with LilyPond. On the one hand, I think I can help you > wrangle your text in the way you want; but I am worried that we are > fighting a bit of the meta problem above. What is this text intended to > represent with regards to music? Microtonal accidentals, some of my own. > What are the semantics of the transformation you want to apply? The string example is an exaggerated but posable string of accidentals, used for a note. Applied as a ligather using a list of strings . > Could there be another way to express > things that might be easier to work with or that might better fit the > patterns of LilyPond? Not for what I want to try. > Mind you, the answers to those questions would be taking us from the > narrow problem into the broader scope. And perhaps only you truly > benefit from answering them. Probably. > > > > Say I have "-y -ax3 +rx2 -stx2 t" "y" could represent one of my accidentals, "-y" inverted. "-ax3" , one of my accidentals , "-a" used 3 times; like a flag, but needs to be inverted to "-x3". "+rx2" needs a space like "+r x2". "t" standard accidental. All "+" removed. "-y -ax3 +rx2 -stx2 t" becomes "-y -a -x3 r x2 -st -x2 t" , then convert to list ("-y" "-a" "-x3" "r" "x2" "-st" "-x2" "t"). > > I wanted, if "-" followed "x" before a space, then replace the "x" with > > " -x" for each.If I use > > "(^-.*)x" "-y -ax3 +rx2 -stx2 t" 'pre 1 " -x" 'post) , from what I > > have read, this would happen at the first "x" from the right. So > > that will not work. > > Ah, there are at least two critical pieces of information I overlooked. > The prefix in question does not necessarily occur at the beginning of > the string. Also, the string we are looking for might occur multiple > times so we want the nearest match. > > We can address this as follows by being more precise in our regular > expression. > > /^/ becomes /(^|\s)/ > > Here we are using alteration to assert that we are either at the > beginning of the string or that we have just seen a whitespace > character. > > /.*/ becomes /[^x]*/ > > All quantifiers in POSIX ERE (and thusly GNU ERE) are greedy, so care > must be taken to limit what can be matched so we do not grab too much. > We do not want an "x" to be matched, so we use a negated character > class. > > Let us see this working: > > > (regexp-substitute/global #f >"((^|\\s)-[^x]*)(x)" >"-y -ax3 +rx2 -stx2 t" >'pre 1 " -" 3 'post) > > > "-y -a -x3 +rx2 -st -x2 t" > > > Seems good. But therein lies the problem with test-driven development > (TDD). Our code will only be as good as our test cases, which is why we > are instructed to "test everything that can possibly break". There is a > hidden problem above due to using /[^x]*/. This matches more than what > we want. In particular, it matches whitespace. > > The first substitution in the above example was not effectively > operating on "-ax3" but rather on "-y -ax3". The end result looks > correct, but what if we had said "-y +ax3"? This would have been > matched because we included whitespace, resulting in "-y +a -x3", which > is presumably incorrect. > > The fix is simple: add [:space:] to the negated character class. Below > I have amended the test string to include "-z +bx4" which should remain > unchanged. > > > (regexp-substitute/global #f >"((^|\\s)-[^x[:space:]]*)(x)" >"-y -ax3 +rx2 -stx2 t -z +bx4" >'pre 1 " -" 3 'post) > > > "-y -a -x3 +rx2 -st -x2 t -z +bx4" > > > What about "-x"? Our expression will match that because we are using > the zero-or-more quantifier (*). The resulting substitution, "- -x", is > probably not what we want. We should ensure that there is at least one > non-"x" character: > > > (regexp-substitute/global #f >"((^|\\s)-[^x[:space:]]+)(x)" >"-y -ax3 +rx2 -stx2 t -z +bx4 -x" >'pre 1 " -" 3 'post) > > > "-y -a -x3 +rx2 -st -x2 t -z +bx4 -x" > > > > > My next step was to convert the string to a list of strings. So if i > > convert first, "
Re: Regexp Functions
On 2020-06-15 11:16 pm, Freeman Gilmore wrote: "y" could represent one of my accidentals, "-y" inverted. "-ax3" , one of my accidentals , "-a" used 3 times; like a flag, but needs to be inverted to "-x3". "+rx2" needs a space like "+r x2". "t" standard accidental. All "+" removed. "-y -ax3 +rx2 -stx2 t" becomes "-y -a -x3 r x2 -st -x2 t" , then convert to list ("-y" "-a" "-x3" "r" "x2" "-st" "-x2" "t"). This is sounding much more like you have invented a language/grammar that needs to be parsed rather than simply wanting to apply some arbitrary text transformation. The interest in string manipulation stems from wanting to support a form of shorthand within the microtonal ligature notation. To that end, allow me to present an alternate way to attack the problem. Consider: (apply append (map (lambda (m) (let ((inv (or (match:substring m 2) ""))) (map (lambda (s) (string-append inv s)) (filter (lambda (s) (not (string-null? s))) (map (lambda (n) (or (match:substring m n) "")) '(3 4)) (list-matches "((-)|\\+?)([a-z]*)(x[0-9]+)?" "-y -ax3 +rx2 -stx2 t"))) ("-y" "-a" "-x3" "r" "x2" "-st" "-x2" "t") Several assumptions are being made within the core regular expression above; but what is important to note is that we are not doing any text substitution of the original input. Rather, we are using a regular expression to identify the valid "words" and extract the key parts of those words. While you can certainly act upon the information right away, I am showing a way to build a list of strings that should meet your specification. -- Aaron Hill
Re: Regexp Functions
On Tue, Jun 16, 2020 at 4:41 AM Aaron Hill wrote: > > On 2020-06-15 11:16 pm, Freeman Gilmore wrote: > > "y" could represent one of my accidentals, "-y" inverted. "-ax3" , > > one of my accidentals , > > "-a" used 3 times; like a flag, but needs to be inverted to "-x3". > > "+rx2" needs a space like "+r x2". "t" standard accidental. All "+" > > removed. > > "-y -ax3 +rx2 -stx2 t" becomes "-y -a -x3 r x2 -st -x2 t" , then > > convert to list > > ("-y" "-a" "-x3" "r" "x2" "-st" "-x2" "t"). > > This is sounding much more like you have invented a language/grammar > that needs to be parsed rather than simply wanting to apply some > arbitrary text transformation. The interest in string manipulation > stems from wanting to support a form of shorthand within the microtonal > ligature notation. > > To that end, allow me to present an alternate way to attack the problem. > Consider: > > > (apply append >(map (lambda (m) > (let ((inv (or (match:substring m 2) ""))) > (map (lambda (s) (string-append inv s)) > (filter >(lambda (s) (not (string-null? s))) >(map (lambda (n) (or (match:substring m n) "")) > '(3 4)) > (list-matches > "((-)|\\+?)([a-z]*)(x[0-9]+)?" > "-y -ax3 +rx2 -stx2 t"))) > > > ("-y" "-a" "-x3" "r" "x2" "-st" "-x2" "t") > \version "2.20.0" #(use-modules (ice-9 regex)) #(define V "-6 -ax3 +rx2 -31x2 t" )% note change, numbers as well used for names of accidentals #(define W (apply append (map (lambda (m) (let ((inv (or (match:substring m 2) ""))) (map (lambda (s) (string-append inv s)) (filter (lambda (s) (not (string-null? s))) (map (lambda (n) (or (match:substring m n) "")) '(3 4)) (list-matches "((-)|\\+?)([0-9]*|[a-z]*)(x[0-9]+)?" % do not that this fool you, this chang is about V %the only thing I think i understand here #(write W) == ("-6" "-a" "-x3" "r" "x2" "-31" "-x2" "t") == I am running out of time, but the mapping may be extended to include a ruffer input that i have already converted to the form like V above using 4 steps. May take awhile to get back. Thank you, ƒg > > Several assumptions are being made within the core regular expression > above; but what is important to note is that we are not doing any text > substitution of the original input. Rather, we are using a regular > expression to identify the valid "words" and extract the key parts of > those words. While you can certainly act upon the information right > away, I am showing a way to build a list of strings that should meet > your specification. > > > -- Aaron Hill
Re: Regexp Functions
On Tue, Jun 16, 2020 at 4:41 AM Aaron Hill wrote: > > On 2020-06-15 11:16 pm, Freeman Gilmore wrote: > > "y" could represent one of my accidentals, "-y" inverted. "-ax3" , > > one of my accidentals , > > "-a" used 3 times; like a flag, but needs to be inverted to "-x3". > > "+rx2" needs a space like "+r x2". "t" standard accidental. All "+" > > removed. > > "-y -ax3 +rx2 -stx2 t" becomes "-y -a -x3 r x2 -st -x2 t" , then > > convert to list > > ("-y" "-a" "-x3" "r" "x2" "-st" "-x2" "t"). > > This is sounding much more like you have invented a language/grammar > that needs to be parsed rather than simply wanting to apply some > arbitrary text transformation. The interest in string manipulation > stems from wanting to support a form of shorthand within the microtonal > ligature notation. > > To that end, allow me to present an alternate way to attack the problem. > Consider: > > > (apply append >(map (lambda (m) > (let ((inv (or (match:substring m 2) ""))) > (map (lambda (s) (string-append inv s)) > (filter >(lambda (s) (not (string-null? s))) >(map (lambda (n) (or (match:substring m n) "")) > '(3 4)) > (list-matches > "((-)|\\+?)([a-z]*)(x[0-9]+)?" > "-y -ax3 +rx2 -stx2 t"))) > > > ("-y" "-a" "-x3" "r" "x2" "-st" "-x2" "t") > > > Several assumptions are being made within the core regular expression > above; but what is important to note is that we are not doing any text > substitution of the original input. Rather, we are using a regular > expression to identify the valid "words" and extract the key parts of > those words. While you can certainly act upon the information right > away, I am showing a way to build a list of strings that should meet > your specification. > > > -- Aaron Hill Aaron: I wish I had more time to stay on this, with the good help I am getting. Looks like mapping is the better way to go.There is some more information to be included if mapping is used that cannot be pleased in as I am learning.Simple string manipulation I can do; but reading about regular expressions does not give me the intuitive ability to manipulate them without a lot more study, with my wrote memory a lot more time.So help with this would be appreciated. Input string: 1. input example \j "-y-ax3rex2 -31x2 es accidentalFlat+6 " 2. “-“ is part of the name of the accidental and can represent the name of an inverted accidental. 3. size of white space is not important 4. the accidentals are separated by “ “, “+”, “ +”; “-“, “ -“ (“-“ is part of the name of the accidental) 5. accidentals are of the form “…” or “-…” with rang [0 - 9]*|[a – z]*|[A – Z]* (abbreviated or full name) 6. accidentals that are applied more than one time can be ligatures “…xn”; “-…xn” , rang of n 2 – 5, (no “-x” or “-n”) Output list of strings: 1. order of accidentals to be maintained 2. “+” are eliminated 3. “-“ are retained (part of name of accidental) 4. accidentals are of the form “…” or “-…” with rang [0 - 9]*|[a – z]*|[A – Z]* 5. “…xn” => “…” “xn” (will form a true ligature) 6. “-…xn” => “-…” “-xn” (will form a true ligature) 7. So far, each accidental has no space between them, add an “SP” between each split except “…” “xn” and “-…” “-xn” (the true ligatures), “Sp” is a narrow blank glyph. 8. Output example: ("-y” “Sp” “-a” “-x3 “Sp” “re” “x2” “Sp” “-st” “-x2” “Sp” “es” “Sp” “accidentalFlat” “Sp” “6”) Simple string manipulation does not get me what I also need to handle the ligatures in the following. "-y-ax3rx2 -31x2 es accidentalFlat+6" => (-y (* -a 3) (* re 2) (* -v31 2) es accidentalFlat +v6) , here the “-“ is not part of the name of the variable, but just give a negative value to it. Note v’s are added to string names that are numbers to create a variable, “+6” => +v6, or => v6, Also +’s can be retained or not. I will then ‘apply +’ to this list. So would this work instead, => (+ -y (* -a 3) (* re 2) (* -v31 2) es accidentalFlat +v6) I have developed a system of accidental that I want to try, and I want the program to be usable for other systems as well. I would be using short names for my accidentals (some numbers if I do not have a problem with them, they work so far). Inverted accidentals have “-“ added to the name of the glyph. An accidental used more than one time is a ligature.The input accidentals are separated by “ “ or “+” and “-“ for inverted accidentals, being part of the name. Thank you, ƒg