Jump to content

Regular Expression


iwato

Recommended Posts

PROBLEM: Consider the following sequence of an unknown number of delimited patterns. "oranges", "apples", "bananas, grapes, and kiwi", . . . , "other fruit"Write a Find command using GREP (Regular Expressions) that isolates the contents of each pair of delimiters, but does not include the delimiters. Write this pattern in such a way that one or more of the resulting aubpatterns can be selected at will and used to replace the content of the original sequence without the delimiters. The number of kinds of fruit in any given subpattern must be retained. What I thought would be a fairly simple task has proven very challenging.Roddy

Link to comment
Share on other sites

Ooh, I remember this!Hint - consider this expression:

/"([^"]+)"/

Link to comment
Share on other sites

Ooh, I remember this!Hint - consider this expression:
/"([^"]+)"/

Alone this does not resolve the problem, but it does add a new vantage point. Find: "([^"]+)" Replace: \1String: "apples", "oranges, kiwis, mangos", "penguins"Result: apples, oranges, kiwis, mangos, penguinsThis is nice insofar as it goes, but it does not yet permit me to replace the entire string with only apples, only oranges, kiwis, mangos, or even only penquins. It claims to have discovered three matches, but only \1 yields anything very useful. \2 and \3 result only in empty commas.I will play with it a little more, and let you know the result.Thanks for your good effort.
Link to comment
Share on other sites

Hint - consider this expression:
/"([^"]+)"/

I have made some progress. The following code will extract each of the words without the quotations as separate words. One need merely change the N to a number. For example, \3 yields kiwis.Text: "apples", "oranges, kiwis, mangos", "penguins"Find: "([\w]+)[", ]+([\w]+)[", ]+([\w]+)[", ]+([\w]+)[", ]+([\w]+)"Replace: \NWhat it does not do is1) Do the same for any number of delimited pairs.2) Render faithfully what is inside each pair of delimiters.Roddy
Link to comment
Share on other sites

Hint - consider this expression:
/"([^"]+)"/

This code will capture everything that I want for this particular string, but it is string-specificFind: "([\w]+)[", ]+(([\w]+)[", ]+([\w]+)[", ]+([\w]+))[", ]+([\w]+)"Replace: \NResult \1: applesResult \2: oranges, kiwis, mangosResult \6: penquinsRoddy
Link to comment
Share on other sites

I think you can sum the repeating parts with something like

"(([\w]+)[", ]+((([\w]+)[", ]+)*([\w]+))[", ]+)*([\w]+)"

i.e. make the whole pattern repeatable zero or more times.

Link to comment
Share on other sites

I think you can sum the repeating parts with something like
"(([\w]+)[", ]+((([\w]+)[", ]+)*([\w]+))[", ]+)*([\w]+)"

i.e. make the whole pattern repeatable zero or more times.

Firstly the above code does not produce a useful result, but thank you for trying anyway.Secondly, when I break it up into its sub-patterns I obtain the following:1) ([\w]+)[", ]+ // This captures the first entity.2) ((([\w]+)[", ]+)*([\w]+))[", ]+)* // What is this one suppose to do?3) ([\w]+) // This captures the last entity.MY GOAL: To be able to 1) capture the content of each set of paired delimiters, as it is, without its delimiters, and2) perform Item 1 for an unknown number of paired delimiters.By the way, I am glad that you were able to resolve your migration problem.Roddy
Link to comment
Share on other sites

Are you allowed to subsequently use anything else than regexes to cnstruct the final string? Because if so, you can store the matches in an array or something, and then just loop over the matches. Is this a UNIX shell we're talking about? I think GREP had a way to export the matches into a file or stream. I don't know enough about such shells to tell you how to loop it there though.

Link to comment
Share on other sites

Because if so, you can store the matches in an array or something, and then just loop over the matches.
For right now the problem is to extract the matches -- not place them. Already, with Synook's helpful hint, I have demonstrated that it can be done for a known number of paired delimiters. Perhaps I could count the number of delimiters present, divide by two, and then write a script that would create a match pattern for the resulting number of paired delimiters. This seems like a lot of work for something that I thought possible solely with regular expression quantifiers.Perhaps I should be satisfied with the progress that I have already made.Certainly, it has taught be quite a lot about regular expressions.Maybe I have exhausted this forum's knowledge of regular expressions.Roddy
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...