Find:

    
<ANIMAL[^>]+?(?:NAME="([^"]+)"[^>]+LIKES="([^"]+)"|LIKES="([^"]+)"[^>]+NAME="([^"]+)"|(?:NAME|LIKES)="([^"]+)")[^>]*>
    
Replace:

    \1 \2 \4 \3 \5
    
Test data :

    <ANIMAL NAME="Fluffy" SPECIES="dog" LIKES="bones" AGE="6">
    <ANIMAL LIKES="Banana" NAME="Abu" SPECIES="Monkey" AGE="6">
    <ANIMAL NAME="Fluffy" SPECIES="dog" LIKES="" AGE="6">
    <ANIMAL LIKES="Banana" NAME="" SPECIES="Monkey" AGE="6">
    <ANIMAL AGE="6" NAME="Peter" SPECIES="Rabbit" >
    <ANIMAL SPECIES="Rabbit" AGE="6" LIKES="Carrots">

Extract:

    Fluffy bones   
      Abu Banana 
        Fluffy
        Banana
        Peter
        Carrots

In case you will want to remove the leading spaces.

Find:

    ^\s+

Replace:

    <empty>
    
If it gets more complicated than that maybe you should go for more advanced 
XML parsing tools.
Regular expressions are not the best fit for those cases.

HTH

Jean Jourdain

On Wednesday, August 25, 2021 at 9:48:50 PM UTC+2 BBLuv wrote:

> Appreciated Jean! It works as if miraculous. If you would accept a further 
> challenge: how can the expression also find instances when only one of the 
> attributes is present? bonus challenge: and if the attributes are not in 
> that order?
> Great big thanks
>
> On Wednesday, August 25, 2021 at 4:53:49 PM UTC+3 jj wrote:
>
>>
>> Hi BBLuv,
>>
>> Find: 
>>
>>     <ANIMAL[^>]+NAME="([^"]*)"[^>]+LIKES="([^"]*)"[^>]*>
>>
>> Replace:
>>
>>     \1 -- \2
>>
>> and use the Extract button of the find window.
>>
>> HTH
>>
>> Jean Jourdain
>>
>> On Wednesday, August 25, 2021 at 2:12:48 PM UTC+2 BBLuv wrote:
>>
>>> Hi - non technical user here, proud to have figured out part of my goal.
>>>
>>> I have multiple XML files that I am trying to scan through to pluck out 
>>> instances of tag attributes. The XML looks like this, and I want to extract 
>>> the NAME and LIKES  attributes across the files. 
>>>
>>> <ANIMAL NAME="Fluffy" SPECIES="dog" LIKES="bones" AGE="6">
>>>
>>> Using grep + extract I am able to produce this:
>>>
>>> search = (?<=NAME=").*?(?=")|(?<=LIKES=").*?(?=")
>>> output = 
>>> Fluffy
>>> bones
>>> Tiger
>>> chewtoys
>>> Rusty
>>> mailmen
>>>             
>>> I would love output that includes my own text:
>>> Fluffy -- bones
>>> Tiger -- chewtoys
>>> Rusty -- mailmen
>>>
>>> or at least has the output values in one line per XML tag
>>> Fluffy bones
>>> Tiger chewtoys
>>> Rusty mailmen
>>>
>>> I learned the pipe character lets me make multiple extractions but I am 
>>> not able to control the output.
>>>
>>> Can you help?
>>>
>>> Thanking you...
>>>
>>>
>>>
>>>

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "[email protected]" rather than 
posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/bf59b3f0-e76f-49b4-9001-f8bf8480e1bdn%40googlegroups.com.

Reply via email to