John Humphreys
John Humphreys

Reputation: 39224

Replace found comma separated items with space separated items using regex

My Problem

I have a large XML file in which someone mistakenly separated some space-separated identifiers with commas in an attribute. There are far too many instances to fix this by hand, so I wanted to do a regex replace.

I can do a non-greedy match of the "i" attribute, but I'm not sure how to do the non-greedy match and combine it with a string replace of comma-to-space.

Non-Greedy Match Regex

i="(.*?)"

Sample Data

<c id="group_1">
    <i id="item_1" i="F:41 F:42 F:D4 G:H3">
        <id="tag_1" />
    </i>
    <i id="item_2" i="R:08,F:42,F:D8,G:H23">
        <id="tag_2" />
    </i>
</c>

Can anyone help with this?

Upvotes: 0

Views: 62

Answers (1)

abc123
abc123

Reputation: 18753

Regex101

Find Regex

([^,]+)(,)?

Replace Regex

\1 

Note: There is a space in the replace regex.

Upvotes: 2

Related Questions