0

Is there a way to use regular expression search and replace function in notepad++ to change ip addresses to a range for below?

With below

RewriteCond %{REMOTE_HOST} ^14.96.0.0/14
RewriteCond %{REMOTE_HOST} ^14.102.0.0/17
RewriteCond %{REMOTE_HOST} ^14.102.128.0/22
RewriteCond %{REMOTE_HOST} ^14.102.160.0/19

To be changed to something like

RewriteCond %{REMOTE_HOST} ^14.96.0.([0-1][0-4])$
RewriteCond %{REMOTE_HOST} ^14.102.0.([0-1][0-7])$
RewriteCond %{REMOTE_HOST} ^14.102.128.([0-2][0-2])$
RewriteCond %{REMOTE_HOST} ^14.102.160.([0-1][0-9])$
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Passionate Engineer
  • 10,034
  • 26
  • 96
  • 168

1 Answers1

1

You can do this by searching for

0/(\d)(\d)

and replacing all with

\([0-\1][0-\2]\)$

but are you sure you're doing the right thing?

([0-2][0-2]) doesn't match the range from 00 to 22, it matches 00, 01, 02, 10, 11, 12, 20, 21 and 22 (and nothing else)...

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • +1, but you need to escape the round brackets in the replacement string (tested, no idea why.) – stema Apr 30 '13 at 06:41
  • Thanks for this so how do you have ranges from 0-22 for example? – Passionate Engineer Apr 30 '13 at 08:03
  • @Jae: That would be `([01]?[0-9]|2[0-2])`. Which makes it impossible to achieve a general solution for your problem using regex alone. – Tim Pietzcker Apr 30 '13 at 08:25
  • I just wanted ip ranges with this kind regex that covers those ranges for each country that I wanted to redirect. Is there some source where I can obtain this? It seems too hard to achieve this – Passionate Engineer Apr 30 '13 at 08:31
  • @Jae: You'll need a programmatic solution. I don't know if you can script Notepad++ - what programming languages do you have at your disposal? – Tim Pietzcker Apr 30 '13 at 08:33
  • @stema round brackets in a Notepad regex replacement invoke processing options. For example the string "(?1found:absent)" will insert "found" if the sub-expression that $1 would insert was matched and with "absent" otherwise. So useful with regex searches such as "(abc)|(def)" to which part matched. See http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html – AdrianHHH Apr 30 '13 at 11:37