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 EMPTYOPTION
Wrap aroundandRegular expressionACTION Click on the
Replace Allbutton, exclusively… and same remark as Scott’s one !
Best Regards,
guy038
-
I had
\Kin 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\Ksolutions here (I tend to capture everything leading up to where the\Kwould 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
\Kwill 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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login