Help with a regular expression that matches something before OR after something else

I have a bunch of XML that has lines that look like

<_char font_name="/ITC Stone Serif Std Bold" italic="true" /> 

but sometimes it looks like

 <_char font_size="88175" italic="true" font_name="/ITC Stone Serif Std Bold" /> 

Here is what i need to do

  • Replace italic = "true" with italic = "false for each line containing ITC Stone Serif Std Bold , regardless of whether it appears before OR after the italic part.

Can this be done with a single regex?

I am not looking for real-time solutions. I just have a ton of XML files that have this β€œerror” in them, and I'm trying to do a global search and replace using PowerGrep, which will require one regular expression. If a script is the only way to do this, then so be it.

+4
source share
5 answers

Simple use of '|' operator satisfies you?

 name="/ITC Stone Sans Std Bold"[^>]italic="(true)"|italic="(true)"[^>]font_name="/ITC Stone Serif Std Bold" 

This should detect any string with the attribute name before after the italic attribute with the value true.

+3
source

Well, in general, using RE for XML parsing is a great idea. But if you really wanted this, the easiest way would be to simply do this in two lines:

 if (/ITC Stone Serif Std Bold/) { s/italic="true"/italic="false"/g; } 
+1
source

In Perl - untested:

 while (<>) { s/italic="true"/italic="false"/ if m%font_name="/ITC Stone Sans Std Bold" italic="true"|italic="true" font_name="/ITC Stone Serif Std Bold"%; print; } 

Very frivolous - a global classifier may be required, a more complex replacement may be required if other parts of the same line may contain italic variants.

Also - a thought - should this opportunity be used so that the designation is uniform, so always put italics in front of (or behind) the font name?

0
source
 Pattern: /(<_char(?=(?:\s+\w+="[^"]*")*?\s+font_name="[^"]*?ITC Stone Serif Std Bold[^"]*")(?:\s+\w+="[^"]*")*?\s+italic=")true(?=")/ Replacement: '$1false' 
0
source

Perl 5.10

Using the new features of Perl 5.10.

 s( <_char \s* [^>]*? \K (?: (?&font) \s+ (?&italic) | (?&italic) \s+ (?&font) ) (?(DEFINE) (?<font>font_name="/ITC[ ]Stone[ ]Serif[ ]Std[ ]Bold") (?<italic>italic="true") ) ){ $+{font} . 'italic="false"' }xge 

Warning: not verified.

0
source

Source: https://habr.com/ru/post/1277700/


All Articles