To the guru of RegExp and how to reduce the number in the line?

Good day!
I have a big XML on. It has different tags, including these: <*x>828<*/x> (several thousand). I want to move all the 120 points left, i.e. I need to replace the above mentioned line to <*x>708<*/x>.

My small experience of unfulfilled programmer tells me that Notepad++ and regular expressions will help me. But how exactly? Please help!

Thank you! :)
October 8th 19 at 01:35
3 answers
October 8th 19 at 01:37
Solution
In PHP:
<?php
$result = preg_replace_callback('/(<[^-->]+>)(\d+)(<[^>]+>)/', function($matches) { return $matches[1] . ($matches[2] - 120) . $matches[3]; } , '<*x>828<*/x> <x>120<*/x> <x>5<*/x>');
var_dump($result);

string(37) "<*x>708<*/x> <x>0<*/x> <*x>-115<*/x>"<br>
P. S. Strange you have tags of some kind, typically a closing tag looks like and not <*/x>
Thanks, I'll try it. Tags strange that their abracompteur not eaten. - Zelda.Grant commented on October 8th 19 at 01:40
Thank you! Works, but reduces the values of any tags. I'm ONLY interested in tag <_x_> (without underscores, I struggle with the parser). Help? :) - Zelda.Grant commented on October 8th 19 at 01:43
'/(<[x^>]+>)(\d+)(<[^>]x+>)/' — did do it! :) Thanks again. - Zelda.Grant commented on October 8th 19 at 01:46
A better way: replace '/(<[^>]+>)(\d+)(<[^>]+>)/' on '/()(\d+)(<\/x>)/', then it will be replaced by the contents of the tags - Mackenzie.Durgan commented on October 8th 19 at 01:49
AGH, ugly Habra parser.
Replace in the above example,' /(<[^>]+>)(\d+)(<[^>]+>)/' on '/(<_x_>)(\d+)(<_\_/_x_>)/', then it will be replaced by the contents of the tags <_x_> <_/_x_>. Tlko worked to remove all underscores. - Mackenzie.Durgan commented on October 8th 19 at 01:52
October 8th 19 at 01:39
If what you brought is required verbatim, here for notepad'a:
Find: (<\*x>)(828)(<\*\/x>)
Replace with: \1708\3
=)))))))) No, there are hundreds of different values :) - Zelda.Grant commented on October 8th 19 at 01:42
Then I'm afraid you have a problem using only the tools and notepad'. He sees only the expression for the rows, that is, finding the number he will work with him, not as a number but as a string. It only remains to write the php script suggested by gabruseva conf above, and feed it your xml. - Zelda.Grant commented on October 8th 19 at 01:45
October 8th 19 at 01:41
XML regexps... Nanan.
# coding: utf-8

import codecs
from lxml import etree

ENCODING = 'utf-8'
XML_SOURCE_FILE = 'source.xml'
XML_OUT_FILE = 'source.out.xml'
VALUE = -120
TAG_NAME = 'id'

with codecs.open(XML_SOURCE_FILE, encoding = ENCODING) as xml_source:
 root = etree.parse(xml_source).getroot()

for tag in root.iter(TAG_NAME):
 tag.text = str(int(tag.text) + VALUE)

with codecs.open(XML_OUT_FILE, 'w', encoding = ENCODING) as xml_out:
 xml_out.write(etree.tostring(root, pretty_print = True, encoding = ENCODING, xml_declaration = True))

And xmllint for prevention remember.

Find more questions by tags ProgrammingAlgorithmsRegular expressions