regex help

  • Thread starter Thread starter g field
  • Start date Start date
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?
 
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
 
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
 
Back
Top