regex to match a variable part of a line?

November 26, 2010 at 19:24:27
Specs: Windows 7

I hope this is a suitable forum where somebody can kindly help me.

If I have a line such as :

<channel service_id="01a3" name="MEO 3D" service_type="0019"/>

I need a regular expression to generically match only the part within the second set of quotes.

The line I have shown is only an example and I have many lines to match which are all of the same syntax.

In case it helps, I have used XXX below to show the parts of the lines which are variable :

<channel service_id="XXX" name="XXX" service_type="XXX"/>

The matching expression must be able to match any combination of characters or spaces within only that second set of quotes.

Letters, if present, will normally be a mixture of upper and lower case but, if it helps to match, I can force them to be upper case before using your suggested match. I will want to make them all upper case anyway.

Alternatively, if the above is not possible, could somebody instead suggest a regex to generically match this part of the line, from the same example :

name="MEO 3D

Note that the end of that match must not include the " after 3D, as correctly shown.

Either way, my aim is to then use


to add some text after the matching expression.

eg to use the same example, my ultimate result is something like :

<channel service_id="01a3" name="MEO 3D Hispasat" service_type="0019"/>

If relevant, the tool / editor I am using to Find / Replace is Open Office Writer and the file is an .xml

Many thanks,



See More: regex to match a variable part of a line?

Report •

November 26, 2010 at 21:00:19
Put your line in a file called MYLINE and try this.

@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=3 delims==" %%a in (myline) do (
set T=%%a
set T=!T: service_type=!
set T=!T:"=!

for /f "tokens=* delims= " %%a in (myline) do (
set S=%%a
set S=!S:%T%=%T% Hispasat!
>> newfile echo.!S!

Life is too important to be taken seriously.


Report •

November 27, 2010 at 08:52:18
Hello Mechanix2go and thanks very much for your reply.

I think you and possibly this forum are working at a higher computing level than me. Nevertheless, I guessed that your code should go in a .bat file and yes, running it produces a 'newfile' with the line completed. Excellent!

I only had the one example line in MYFILE but I assume when I try it later using my original file's whole contents, it should match and replace all the lines.

Just to educate me a little; is this using 'regular expressions' or have you used a specific coding language?

Many thanks,

Report •

November 28, 2010 at 07:56:59

Yes, this is strictly NT/XP batch. There is a built-in regex tool called FINDSTR but I never learned it.

And yes, it will work for multiple lines.

BUT [Here comes the but part.] It's hard wired for service_type.

Life is too important to be taken seriously.


Report •

Related Solutions

November 28, 2010 at 19:00:35
"It's hard wired for service_type."

Do you mean that any matching lines have to feature the section :
where XXX can be anything

If so, that's ok.

(I haven't actually tested a full file match yet because I've been bogged down with other things)

Thanks again,

Report •

November 28, 2010 at 20:25:21

yes, service_type="XXX"

Life is too important to be taken seriously.


Report •

November 29, 2010 at 06:38:56
ok, thanks.

I've tried it now on a full or partial file, but only the last line seems to get matched and replaced.

Here is an example section of the original file I am working with, which I have cut and pasted into MYLINE before running the .bat :

<?xml version="1.0" encoding="UTF-8"?>
	<sat name="Astra 2A / Astra 2B / Astra 2D / Eurobird 1 (28.2?" diseqc="0" position="282">
		<transponder id="07d1" onid="0002" frequency="11719000" inversion="2" symbol_rate="29500000" fec_inner="3" polarization="0">
			<channel service_id="0f26" name="Sky Sports HD2" service_type="0019"/>
			<channel service_id="0f29" name="Sky Sports HD2" service_type="0019"/>
			<channel service_id="0ede" name="MTVN HD" service_type="0019"/>
			<channel service_id="0ef9" name="SkyShowcseHD" service_type="0019"/>
		<transponder id="07d2" onid="0002" frequency="11740000" inversion="2" symbol_rate="27500000" fec_inner="2" polarization="1">
			<channel service_id="2333" name="6008" service_type="0001"/>
			<channel service_id="1771" name="LIVING" service_type="0001"/>
			<channel service_id="1772" name="Challenge" service_type="0001"/>
			<channel service_id="1773" name="LIVING Loves" service_type="0001"/>
			<channel service_id="1774" name="Bravo" service_type="0001"/>
			<channel service_id="1775" name="LIVING +1" service_type="0001"/>
			<channel service_id="1776" name="Bravo+1" service_type="0001"/>
			<channel service_id="1777" name="LIVINGit" service_type="0001"/>
			<channel service_id="1778" name="Channel One" service_type="0001"/>
			<channel service_id="177c" name="Challenge+" service_type="0001"/>
			<channel service_id="177e" name="Bravo+" service_type="0001"/>
			<channel service_id="1788" name="Bravo~" service_type="0001"/>

In that example, the only line which gets changed is :

<channel service_id="1788" name="Bravo~" service_type="0001"/>

which becomes :
<channel service_id="1788" name="Bravo~ Hispasat" service_type="0001"/>

Do you know what's going wrong? I continue to be very grateful for your help!


(PS In case you've noticed that the file I'm working with is a satellite transponder
list, I'm aware that adding 'Hispasat' in the example shown is wrong and
would be something like 'Astra 2'. Ignore that, it's just an example.)

Report •

November 29, 2010 at 23:33:46
@echo off > newfile & setLocal enableDELAYedeXpansion

set O=" service_type
set T=MYnewTXT

for /f "tokens=* delims=" %%a in (leesfile) do (
set S=%%a
set S=!S:%O%= %T%%O%!
) >> newfile

Life is too important to be taken seriously.


Report •

November 30, 2010 at 14:16:24
Thanks Mechanix2go,

I appreciate your help again and that is working as planned.

I still have some other problems regarding how to actually manipulate the data successfully, but they are beyond the scope of my specific query here which you have solved.

Briefly, it appears I can't simply copy/paste the contents of the file into a simple text file, run your batch file, then copy/paste the results back into the original file. There are formatting issues which I was previously unaware of.

It now appears I have to abandon my initial plans and instead manipulate the data in the form of an excel spreadsheet. I can source the original file in such a format but I am using Open Office rather than the payware Excel.
This is causing me a further learning curve and compatibilty issues with the editor which I am using to export / import the master files to / from the satellite receiver.

All this effort to add the satellite name to channels in a channel list and then eventually make the names upper case! I must be mad .... !



Report •

December 1, 2010 at 23:28:21
Hello again Mechanix2go if you're still here,

It appears I was confused / getting bogged down; I can actually run your .bat file over my original file services.xml and the resultant newfile can be changed to services.xml
This seems not to break any formatting or structure in the xml file.
My formatting and structure problems / misunderstandings seem to happen when I try to edit the .xml file in Open Office than re-save it. So, luckily it appears I can avoid that.

To conclude my aim, could you possibly help me one final time?
What I want to do is force alphabetical characters to be upper case.
This is to only be applied to the variable part shown below as X's :

<channel service_id="01a3" name="XXX XX" service_type="0019"/>

The same criteria apply that the text between those quotes can be any combination of letters, numbers, spaces or other characters such as - or ! or ?
(although previously I didn't offer so much detail on that)

If it is possible, I don't know if you can include it in the code you offered in post 7, or if I/we have to use a fresh .bat file.
Ideally, I don't want the text we are already successfully inserting to be changed to upper case. So presumably any new code or new .bat file will have to run before the text insertion.

So, I hope you can kindly help me again. This is the final part of my aim and I don't forsee any further need to come back and ask you to spoon-feed me even more code. If this was an ongoing and extensive project, I would have to persevere with learning how to do these things myself. I have actually looked into some XML editors and XML DOM parsers, but have struggled to learn how to use them.

Thanks again,

Report •

December 2, 2010 at 02:38:40
Hi Lee, If that "XXX XX" can have ! we may be out of luck.

But before we tackle that, would it hurt to uppercase verything. [Before adding the new string?

Life is too important to be taken seriously.


Report •

December 2, 2010 at 10:16:53
Hello once again!

Do you mean uppercase the whole file? I think that would probably break it. I could try these things myself if only I knew how to import / export / edit the file without breaking it. I have been trying to get help from a satellite TV forum regarding the formatting of the files, so far with limited success.

Maybe I can back-track here and give you the whole picture which I probably should have right at the start. It might help or throw up some new ideas :-

At the urls below are 3 files; services.xml, bouquets.xml and satellites.xml
services.xml is the one we are editing, but the 3 files come together to form the complete file structure required by the channel editor/importer/exporter (Dreamset or IHAD Dreamboxedit) and satellite receiver (VU+ Duo).

services.xml contains channel details for 4 satellites; Astra1, Astra2, Hotbird and Hispasat.
My ultimate aims are, in whichever order is practical :

Add the relevant satellite name (after a space) to every channel name. eg that insertion text would be either

Ideally, the case of those insertions would be as shown above.

Force all the original channel names to be upper case.

I am very grateful for your continued help, but if the above just isn't possible within your coding methods, I will probably have to get my brain in gear and learn how to use XML editors or an 'XML DOM parser' which might be some ways to do this properly.


Report •

December 2, 2010 at 22:40:30
Hello again Mechanix2go,

I've managed to achieve my aims, partly by finally learning some key aspects of the formatting and editing of spreadsheets in Open Office Calc. It is however a very convoluted process. If I had more than 4 satellites to process, the solution I've found would be crazy to do.

If you think you can conjour up a solution as described earlier and still have the will to do so, it will probably be simpler than my solution!
If not, thanks for the help you've given to my little project.



Report •

December 3, 2010 at 07:59:14
You should do this in a language that knows how to parse and build xml files. Such as Perl or Python. A batch file would not be the appropriate choice.

Report •

Ask Question