regex help

G

g field

I am having trouble with a regex expression - any help would be
appreciated.

I have text files that need to be converted to XML. Most of it works
fine. However, there is one glitch. One section of the text file
looks like this:

Dealer Installed Accessories:
All-weather floor mats
Cargo net

(Note: there could be any number of Accessories listed)

I search for this pattern using:

Dealer.*Accessories: *(\r\n){0,3}(.*) *\r\n*

Or,

Dealer.*Accessories:( *(\r\n){0,3}(.*) *\r\n*).*?

My replace text is <option>$2</option>

The result is always: <option>All-weather floor mats</option>Cargo net


The desired result is <option>All-weather floor
mats</option><option>Cargo net</option>

(Of course, that pattern will need to repeat for all the Accessories
listed in the file).

Any thoughts on how to tweak the regex expression to accommodate this?
 
N

Niki Estner

g field said:
I am having trouble with a regex expression - any help would be
appreciated.

I have text files that need to be converted to XML. Most of it works
fine. However, there is one glitch. One section of the text file
looks like this:

Dealer Installed Accessories:
All-weather floor mats
Cargo net

(Note: there could be any number of Accessories listed)

How do you know the end of accessories list? Do you want every line after
that to be matched?
...
Any thoughts on how to tweak the regex expression to accommodate this?

You can probably do this by a look-behind test that checks if "Dealter
Installed Accessories" can be found somewhere before the match.
"(?<=^Dealer.*Accessories:\s*\r\n(?:.*\r\n)*)(.*)\r\n"

But I think this might be quite slow if files are big.
If speed matters, consider using two expressions: one to find the "Dealer
Installed Accessories...End" blocks, and another one to do the substitutions
in that block.

Niki
 
G

Gilad Field

thanks - that was helpful. for this case your
"(?<=^Dealer.*Accessories:\s*\r\n(?:.*\r\n)*)(.*)\r\n" suggestion was
very good. I know the files won't ever get very large!

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Regex question (easy?) 5
regex help 1
HowTo? RegEx - pattern to exclude the whole word 4
Help with Regex 1
Regex problem with with carriage returns 1
Simple Regex Replace() question. 2
Regex Help 1
Regex Help 2

Top