Trim xml attribute
-
I have a large group of xml files that were generated to a specification where one attribute
dealerCustName=“This is the attribute there are many of them”
I need to be able to Trim the value of all of these attributes to 25 characters.
dealerCustName = “123456789012345678901234567890123456789”
needs to be this
dealerCustName = “1234567890123456789012345”
The attribute needs to be truncated to 25 chars regardless of the content.
-
One way would be thus:
Find what zone:
(?-i)(dealerCustName\s*=\s*")([^"]{25})[^"]+"
Replace with zone:\1\2"
Search mode: Regular expressionIf you can’t figure that out, just say so and it will be explained. :-D
-
Hello, @mrkhad123,
An other formulation could be :
SEARCH
(?-i)^dealerCustName\h*=\h*"\d{25}\K\d*
REPLACE
Leave EMPTY
OPTION
Wrap around
andRegular expression
ACTION Click on the
Replace All
button, exclusively… and same remark as Scott’s one !
Best Regards,
guy038
-
I had
\K
in my regular expression at first as I played wit it. Then I remembered that Notepad++ has trouble sometimes with it when using the “walking replace” (definition: using Replace to walk through and selectively replace text). I think most people (especially regex newbies) prefer to walk through some replacements first to build confidence that a search expression is correct before hitting Replace All. Thus I tend to not propose\K
solutions here (I tend to capture everything leading up to where the\K
would be into a group, and then use that group at replacement time to re-insert the text).Of course, maybe this is a case where
\K
will work with the walking-replace; not sure, didn’t try. ;-) -
@Scott-Sumner Beautiful! That is exactly what I was looking for! Is there a place where I can find syntax for these types of replacements?
-
Some good references relating specifically to Notepad++ are:
- general: http://docs.notepad-plus-plus.org/index.php/Regular_Expressions
- find-specific: http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html
- replace-specific: http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html
Also, some other general references I like are:
-
Thanks again Scott.
You have been of great help.