Is possible to get elements from XML using Notepad++ Regex? [duplicate]RegEx match open tags except XHTML self-contained tagsWhat do 'lazy' and 'greedy' mean in the context of regular expressions?Can you provide some examples of why it is hard to parse XML and HTML with a regex?Retrieve XML Namespaces using RegexDeclaring a custom android UI element using XMLHow do I format XML in Notepad++?How To Auto-Format / Indent XML/HTML in Notepad++Replacing xml tag with generated image (python regex)Getting the rootelement from a streamed xmlUse RegEx to execute multiple find/replace commands in Notepad++ in one clickSQL replace @lt and @gt with < and >UltraEdit/Notepad - XML Remove nodes with empty properties
Can an old DSLR be upgraded to match modern smartphone image quality
Do adult Russians normally hand-write Cyrillic as cursive or as block letters?
What people are called boars ("кабан") and why?
Restoring order in a deck of playing cards (II)
Beginner's snake game using PyGame
What is a simple, physical situation where complex numbers emerge naturally?
Is there any Biblical Basis for 400 years of silence between Old and New Testament?
Metal bar on DMM PCB
GFCI Outlet in Bathroom, Lights not working
Incremental Ranges!
Creating Fictional Slavic Place Names
Old black and white movie: glowing black rocks slowly turn you into stone upon touch
Sucuri detects malware on wordpress but I can't find the malicious code
How can I offer a test ride while selling a bike?
Other deep learning image generation techniques besides GANs?
The term for the person/group a political party aligns themselves with to appear concerned about the general public
What's the most polite way to tell a manager "shut up and let me work"?
Does it cost a spell slot to cast a spell from a Ring of Spell Storing?
How should I push back against my job assigning "homework"?
Anyone teach web development? How do you assess it?
Is having a hidden directory under /etc safe?
Is it possible for people to live in the eye of a permanent hypercane?
How can a single Member of the House block a Congressional bill?
Is it possible to kill all life on Earth?
Is possible to get elements from XML using Notepad++ Regex? [duplicate]
RegEx match open tags except XHTML self-contained tagsWhat do 'lazy' and 'greedy' mean in the context of regular expressions?Can you provide some examples of why it is hard to parse XML and HTML with a regex?Retrieve XML Namespaces using RegexDeclaring a custom android UI element using XMLHow do I format XML in Notepad++?How To Auto-Format / Indent XML/HTML in Notepad++Replacing xml tag with generated image (python regex)Getting the rootelement from a streamed xmlUse RegEx to execute multiple find/replace commands in Notepad++ in one clickSQL replace @lt and @gt with < and >UltraEdit/Notepad - XML Remove nodes with empty properties
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
RegEx match open tags except XHTML self-contained tags
34 answers
I have an XML with different Item
's which may contain the attribute Setting
named SerialNumber
. Im trying to get all the item names followed with the serial number.
My approch is using Notepad++ Regex, to get the name of the Item
and the value of the attribute Setting
named SerialNumber
something like this:
Sender0;3990 Sender3;4444 Sender4;7774
But trying it the only thing i can get is that notepad++ selects all the text... My fast approach was something like this:
^<Item Name="(.*)" Category=".*<Setting Name="SerialNumber">(.*)</Setting>.*</Item>
And replace:
(1);(2)
The XML:
<Item Name="Sender0" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">3990</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender1" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting>...</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender2" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting>...</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender3" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">4444</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender4" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">7774</Setting>
<Setting>...</Setting>
</Item>
Hope you can help me, thanks :)
regex xml notepad++
marked as duplicate by Wiktor Stribiżew, Ken White, Toto
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 24 at 16:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 3 more comments
This question already has an answer here:
RegEx match open tags except XHTML self-contained tags
34 answers
I have an XML with different Item
's which may contain the attribute Setting
named SerialNumber
. Im trying to get all the item names followed with the serial number.
My approch is using Notepad++ Regex, to get the name of the Item
and the value of the attribute Setting
named SerialNumber
something like this:
Sender0;3990 Sender3;4444 Sender4;7774
But trying it the only thing i can get is that notepad++ selects all the text... My fast approach was something like this:
^<Item Name="(.*)" Category=".*<Setting Name="SerialNumber">(.*)</Setting>.*</Item>
And replace:
(1);(2)
The XML:
<Item Name="Sender0" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">3990</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender1" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting>...</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender2" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting>...</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender3" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">4444</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender4" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">7774</Setting>
<Setting>...</Setting>
</Item>
Hope you can help me, thanks :)
regex xml notepad++
marked as duplicate by Wiktor Stribiżew, Ken White, Toto
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 24 at 16:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Regex + XML = evil evil evil ... not my downvote, but regex in NPP is not the best too to use here. Look into using an XML parser.
– Tim Biegeleisen
Mar 20 at 15:05
1
Tim is right. What other technologies can you use? For example, would Powershell be an option?
– Tomalak
Mar 20 at 15:16
2
Regex isn't working because regex cannot be used with XML. Use one of the tools that have been made for XML processing, they exist for a reason.
– Tomalak
Mar 20 at 15:31
2
Indeed, yes. If processing this file is your task, I suggest Powershell because that approach has zero external dependencies in Windows. If you just chose it as a way to learn regex, I suggest working with something other than XML.
– Tomalak
Mar 20 at 15:41
1
Obligatory link about the futility of trying to parse X/HTML with regexes
– Ken White
Mar 20 at 17:27
|
show 3 more comments
This question already has an answer here:
RegEx match open tags except XHTML self-contained tags
34 answers
I have an XML with different Item
's which may contain the attribute Setting
named SerialNumber
. Im trying to get all the item names followed with the serial number.
My approch is using Notepad++ Regex, to get the name of the Item
and the value of the attribute Setting
named SerialNumber
something like this:
Sender0;3990 Sender3;4444 Sender4;7774
But trying it the only thing i can get is that notepad++ selects all the text... My fast approach was something like this:
^<Item Name="(.*)" Category=".*<Setting Name="SerialNumber">(.*)</Setting>.*</Item>
And replace:
(1);(2)
The XML:
<Item Name="Sender0" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">3990</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender1" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting>...</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender2" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting>...</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender3" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">4444</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender4" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">7774</Setting>
<Setting>...</Setting>
</Item>
Hope you can help me, thanks :)
regex xml notepad++
This question already has an answer here:
RegEx match open tags except XHTML self-contained tags
34 answers
I have an XML with different Item
's which may contain the attribute Setting
named SerialNumber
. Im trying to get all the item names followed with the serial number.
My approch is using Notepad++ Regex, to get the name of the Item
and the value of the attribute Setting
named SerialNumber
something like this:
Sender0;3990 Sender3;4444 Sender4;7774
But trying it the only thing i can get is that notepad++ selects all the text... My fast approach was something like this:
^<Item Name="(.*)" Category=".*<Setting Name="SerialNumber">(.*)</Setting>.*</Item>
And replace:
(1);(2)
The XML:
<Item Name="Sender0" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">3990</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender1" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting>...</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender2" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting>...</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender3" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">4444</Setting>
<Setting>...</Setting>
</Item>
<Item Name="Sender4" Category="" ClassName="Cars" Schedule="" Enabled="true">
<Setting>...</Setting>
<Setting Name="SerialNumber">7774</Setting>
<Setting>...</Setting>
</Item>
Hope you can help me, thanks :)
This question already has an answer here:
RegEx match open tags except XHTML self-contained tags
34 answers
regex xml notepad++
regex xml notepad++
asked Mar 20 at 15:04
RalshoRalsho
271212
271212
marked as duplicate by Wiktor Stribiżew, Ken White, Toto
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 24 at 16:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Wiktor Stribiżew, Ken White, Toto
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 24 at 16:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Regex + XML = evil evil evil ... not my downvote, but regex in NPP is not the best too to use here. Look into using an XML parser.
– Tim Biegeleisen
Mar 20 at 15:05
1
Tim is right. What other technologies can you use? For example, would Powershell be an option?
– Tomalak
Mar 20 at 15:16
2
Regex isn't working because regex cannot be used with XML. Use one of the tools that have been made for XML processing, they exist for a reason.
– Tomalak
Mar 20 at 15:31
2
Indeed, yes. If processing this file is your task, I suggest Powershell because that approach has zero external dependencies in Windows. If you just chose it as a way to learn regex, I suggest working with something other than XML.
– Tomalak
Mar 20 at 15:41
1
Obligatory link about the futility of trying to parse X/HTML with regexes
– Ken White
Mar 20 at 17:27
|
show 3 more comments
3
Regex + XML = evil evil evil ... not my downvote, but regex in NPP is not the best too to use here. Look into using an XML parser.
– Tim Biegeleisen
Mar 20 at 15:05
1
Tim is right. What other technologies can you use? For example, would Powershell be an option?
– Tomalak
Mar 20 at 15:16
2
Regex isn't working because regex cannot be used with XML. Use one of the tools that have been made for XML processing, they exist for a reason.
– Tomalak
Mar 20 at 15:31
2
Indeed, yes. If processing this file is your task, I suggest Powershell because that approach has zero external dependencies in Windows. If you just chose it as a way to learn regex, I suggest working with something other than XML.
– Tomalak
Mar 20 at 15:41
1
Obligatory link about the futility of trying to parse X/HTML with regexes
– Ken White
Mar 20 at 17:27
3
3
Regex + XML = evil evil evil ... not my downvote, but regex in NPP is not the best too to use here. Look into using an XML parser.
– Tim Biegeleisen
Mar 20 at 15:05
Regex + XML = evil evil evil ... not my downvote, but regex in NPP is not the best too to use here. Look into using an XML parser.
– Tim Biegeleisen
Mar 20 at 15:05
1
1
Tim is right. What other technologies can you use? For example, would Powershell be an option?
– Tomalak
Mar 20 at 15:16
Tim is right. What other technologies can you use? For example, would Powershell be an option?
– Tomalak
Mar 20 at 15:16
2
2
Regex isn't working because regex cannot be used with XML. Use one of the tools that have been made for XML processing, they exist for a reason.
– Tomalak
Mar 20 at 15:31
Regex isn't working because regex cannot be used with XML. Use one of the tools that have been made for XML processing, they exist for a reason.
– Tomalak
Mar 20 at 15:31
2
2
Indeed, yes. If processing this file is your task, I suggest Powershell because that approach has zero external dependencies in Windows. If you just chose it as a way to learn regex, I suggest working with something other than XML.
– Tomalak
Mar 20 at 15:41
Indeed, yes. If processing this file is your task, I suggest Powershell because that approach has zero external dependencies in Windows. If you just chose it as a way to learn regex, I suggest working with something other than XML.
– Tomalak
Mar 20 at 15:41
1
1
Obligatory link about the futility of trying to parse X/HTML with regexes
– Ken White
Mar 20 at 17:27
Obligatory link about the futility of trying to parse X/HTML with regexes
– Ken White
Mar 20 at 17:27
|
show 3 more comments
1 Answer
1
active
oldest
votes
I think regex is viable for this. Unless you are missing some details on the question.
Try with this:
Search: s*<Items*Name="([^"]+)"[^>]+>(?:s*<Setting>.*?</Setting>)*(?:s*<Setting Name="SerialNumber">(.*?)</Setting>)?(?:s*<Setting>.*?</Setting>)*s*</Item>
Replace by: (?21;2 )
In notepadd++, the output of your given input would be: Sender0;3990 Sender3;4444 Sender4;7774
NOTE: Do not use . matches new line
option. Also, use match case
if you need so.
Explanation:
s* # 0 or more spaces (space, tab, new line...)
<Item # Literal '<item'
s*
Name=" # Literal 'Name="'
([^"]+) # Any non (") character repeated one or more times
# stored on the first capturing group
" # Literal "
[^>]+ # Any non (>) character repeated one or more
> # Literal >
# After searching for Item Name, there must exists its serial number.
# The serialNumber may be sorrounded by other settings, so We will search:
# perhapsSomeSettings + serialNumber + perhapsSomeSettings
# so that we will be able to find (if exists) the serial number wether it
# is placed as the first, last or middle tag.
(?: # group
s*
<Setting>.*?</Setting>
)* # repeat 0 or more
(?: # This 'setting' group will have the serial number
s*
<Setting Name="SerialNumber">
(.*?) # We capture the data (second capturing group)
</Setting>
)? # Optional
(?:
s*
<Setting>
.*?
</Setting>
)*
s*
</Item>
Please, see also this about greedy/lazy quantifiers.
For the replacement we use (?21;2 )
(?2)
is special syntax in notepadd++ (boost) regexes. It means that if the second capturing group exists, then what's inside is applied. So in our case (?21;2 )
if the second capturing group exists our replacement will be first capturing group (name), ;
and second capturing group (serialNumber)
If you can explain a little bit your regex i'll be extremely grateful, for learning purposes and prevent people like me to fall in this kind of questions :)
– Ralsho
Mar 25 at 8:59
Sure, @Ralsho. I edited my answert to add the explanation.
– Julio
Mar 25 at 10:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think regex is viable for this. Unless you are missing some details on the question.
Try with this:
Search: s*<Items*Name="([^"]+)"[^>]+>(?:s*<Setting>.*?</Setting>)*(?:s*<Setting Name="SerialNumber">(.*?)</Setting>)?(?:s*<Setting>.*?</Setting>)*s*</Item>
Replace by: (?21;2 )
In notepadd++, the output of your given input would be: Sender0;3990 Sender3;4444 Sender4;7774
NOTE: Do not use . matches new line
option. Also, use match case
if you need so.
Explanation:
s* # 0 or more spaces (space, tab, new line...)
<Item # Literal '<item'
s*
Name=" # Literal 'Name="'
([^"]+) # Any non (") character repeated one or more times
# stored on the first capturing group
" # Literal "
[^>]+ # Any non (>) character repeated one or more
> # Literal >
# After searching for Item Name, there must exists its serial number.
# The serialNumber may be sorrounded by other settings, so We will search:
# perhapsSomeSettings + serialNumber + perhapsSomeSettings
# so that we will be able to find (if exists) the serial number wether it
# is placed as the first, last or middle tag.
(?: # group
s*
<Setting>.*?</Setting>
)* # repeat 0 or more
(?: # This 'setting' group will have the serial number
s*
<Setting Name="SerialNumber">
(.*?) # We capture the data (second capturing group)
</Setting>
)? # Optional
(?:
s*
<Setting>
.*?
</Setting>
)*
s*
</Item>
Please, see also this about greedy/lazy quantifiers.
For the replacement we use (?21;2 )
(?2)
is special syntax in notepadd++ (boost) regexes. It means that if the second capturing group exists, then what's inside is applied. So in our case (?21;2 )
if the second capturing group exists our replacement will be first capturing group (name), ;
and second capturing group (serialNumber)
If you can explain a little bit your regex i'll be extremely grateful, for learning purposes and prevent people like me to fall in this kind of questions :)
– Ralsho
Mar 25 at 8:59
Sure, @Ralsho. I edited my answert to add the explanation.
– Julio
Mar 25 at 10:29
add a comment |
I think regex is viable for this. Unless you are missing some details on the question.
Try with this:
Search: s*<Items*Name="([^"]+)"[^>]+>(?:s*<Setting>.*?</Setting>)*(?:s*<Setting Name="SerialNumber">(.*?)</Setting>)?(?:s*<Setting>.*?</Setting>)*s*</Item>
Replace by: (?21;2 )
In notepadd++, the output of your given input would be: Sender0;3990 Sender3;4444 Sender4;7774
NOTE: Do not use . matches new line
option. Also, use match case
if you need so.
Explanation:
s* # 0 or more spaces (space, tab, new line...)
<Item # Literal '<item'
s*
Name=" # Literal 'Name="'
([^"]+) # Any non (") character repeated one or more times
# stored on the first capturing group
" # Literal "
[^>]+ # Any non (>) character repeated one or more
> # Literal >
# After searching for Item Name, there must exists its serial number.
# The serialNumber may be sorrounded by other settings, so We will search:
# perhapsSomeSettings + serialNumber + perhapsSomeSettings
# so that we will be able to find (if exists) the serial number wether it
# is placed as the first, last or middle tag.
(?: # group
s*
<Setting>.*?</Setting>
)* # repeat 0 or more
(?: # This 'setting' group will have the serial number
s*
<Setting Name="SerialNumber">
(.*?) # We capture the data (second capturing group)
</Setting>
)? # Optional
(?:
s*
<Setting>
.*?
</Setting>
)*
s*
</Item>
Please, see also this about greedy/lazy quantifiers.
For the replacement we use (?21;2 )
(?2)
is special syntax in notepadd++ (boost) regexes. It means that if the second capturing group exists, then what's inside is applied. So in our case (?21;2 )
if the second capturing group exists our replacement will be first capturing group (name), ;
and second capturing group (serialNumber)
If you can explain a little bit your regex i'll be extremely grateful, for learning purposes and prevent people like me to fall in this kind of questions :)
– Ralsho
Mar 25 at 8:59
Sure, @Ralsho. I edited my answert to add the explanation.
– Julio
Mar 25 at 10:29
add a comment |
I think regex is viable for this. Unless you are missing some details on the question.
Try with this:
Search: s*<Items*Name="([^"]+)"[^>]+>(?:s*<Setting>.*?</Setting>)*(?:s*<Setting Name="SerialNumber">(.*?)</Setting>)?(?:s*<Setting>.*?</Setting>)*s*</Item>
Replace by: (?21;2 )
In notepadd++, the output of your given input would be: Sender0;3990 Sender3;4444 Sender4;7774
NOTE: Do not use . matches new line
option. Also, use match case
if you need so.
Explanation:
s* # 0 or more spaces (space, tab, new line...)
<Item # Literal '<item'
s*
Name=" # Literal 'Name="'
([^"]+) # Any non (") character repeated one or more times
# stored on the first capturing group
" # Literal "
[^>]+ # Any non (>) character repeated one or more
> # Literal >
# After searching for Item Name, there must exists its serial number.
# The serialNumber may be sorrounded by other settings, so We will search:
# perhapsSomeSettings + serialNumber + perhapsSomeSettings
# so that we will be able to find (if exists) the serial number wether it
# is placed as the first, last or middle tag.
(?: # group
s*
<Setting>.*?</Setting>
)* # repeat 0 or more
(?: # This 'setting' group will have the serial number
s*
<Setting Name="SerialNumber">
(.*?) # We capture the data (second capturing group)
</Setting>
)? # Optional
(?:
s*
<Setting>
.*?
</Setting>
)*
s*
</Item>
Please, see also this about greedy/lazy quantifiers.
For the replacement we use (?21;2 )
(?2)
is special syntax in notepadd++ (boost) regexes. It means that if the second capturing group exists, then what's inside is applied. So in our case (?21;2 )
if the second capturing group exists our replacement will be first capturing group (name), ;
and second capturing group (serialNumber)
I think regex is viable for this. Unless you are missing some details on the question.
Try with this:
Search: s*<Items*Name="([^"]+)"[^>]+>(?:s*<Setting>.*?</Setting>)*(?:s*<Setting Name="SerialNumber">(.*?)</Setting>)?(?:s*<Setting>.*?</Setting>)*s*</Item>
Replace by: (?21;2 )
In notepadd++, the output of your given input would be: Sender0;3990 Sender3;4444 Sender4;7774
NOTE: Do not use . matches new line
option. Also, use match case
if you need so.
Explanation:
s* # 0 or more spaces (space, tab, new line...)
<Item # Literal '<item'
s*
Name=" # Literal 'Name="'
([^"]+) # Any non (") character repeated one or more times
# stored on the first capturing group
" # Literal "
[^>]+ # Any non (>) character repeated one or more
> # Literal >
# After searching for Item Name, there must exists its serial number.
# The serialNumber may be sorrounded by other settings, so We will search:
# perhapsSomeSettings + serialNumber + perhapsSomeSettings
# so that we will be able to find (if exists) the serial number wether it
# is placed as the first, last or middle tag.
(?: # group
s*
<Setting>.*?</Setting>
)* # repeat 0 or more
(?: # This 'setting' group will have the serial number
s*
<Setting Name="SerialNumber">
(.*?) # We capture the data (second capturing group)
</Setting>
)? # Optional
(?:
s*
<Setting>
.*?
</Setting>
)*
s*
</Item>
Please, see also this about greedy/lazy quantifiers.
For the replacement we use (?21;2 )
(?2)
is special syntax in notepadd++ (boost) regexes. It means that if the second capturing group exists, then what's inside is applied. So in our case (?21;2 )
if the second capturing group exists our replacement will be first capturing group (name), ;
and second capturing group (serialNumber)
edited Mar 25 at 10:46
answered Mar 24 at 12:32
JulioJulio
2,3401623
2,3401623
If you can explain a little bit your regex i'll be extremely grateful, for learning purposes and prevent people like me to fall in this kind of questions :)
– Ralsho
Mar 25 at 8:59
Sure, @Ralsho. I edited my answert to add the explanation.
– Julio
Mar 25 at 10:29
add a comment |
If you can explain a little bit your regex i'll be extremely grateful, for learning purposes and prevent people like me to fall in this kind of questions :)
– Ralsho
Mar 25 at 8:59
Sure, @Ralsho. I edited my answert to add the explanation.
– Julio
Mar 25 at 10:29
If you can explain a little bit your regex i'll be extremely grateful, for learning purposes and prevent people like me to fall in this kind of questions :)
– Ralsho
Mar 25 at 8:59
If you can explain a little bit your regex i'll be extremely grateful, for learning purposes and prevent people like me to fall in this kind of questions :)
– Ralsho
Mar 25 at 8:59
Sure, @Ralsho. I edited my answert to add the explanation.
– Julio
Mar 25 at 10:29
Sure, @Ralsho. I edited my answert to add the explanation.
– Julio
Mar 25 at 10:29
add a comment |
3
Regex + XML = evil evil evil ... not my downvote, but regex in NPP is not the best too to use here. Look into using an XML parser.
– Tim Biegeleisen
Mar 20 at 15:05
1
Tim is right. What other technologies can you use? For example, would Powershell be an option?
– Tomalak
Mar 20 at 15:16
2
Regex isn't working because regex cannot be used with XML. Use one of the tools that have been made for XML processing, they exist for a reason.
– Tomalak
Mar 20 at 15:31
2
Indeed, yes. If processing this file is your task, I suggest Powershell because that approach has zero external dependencies in Windows. If you just chose it as a way to learn regex, I suggest working with something other than XML.
– Tomalak
Mar 20 at 15:41
1
Obligatory link about the futility of trying to parse X/HTML with regexes
– Ken White
Mar 20 at 17:27