views:

589

answers:

4

Hi

I'd like to use the search & replace dialogue in UltraEdit (Perl Compatible Regular Expressions) to format a list of IPs into a standard Format.

The list contains:

192.168.1.1  
123.231.123.2  
23.44.193.21

It should be formatted like this:

192.168.001.001  
123.231.123.002  
023.044.193.021

The RegEx from http://www.regextester.com/regular+expression+examples.html for IPv4 in the PCRE-Format is not working properly:

^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]){3}$

I'm stucked. Does anybody have a proper solution which works in UltraEdit?

Thanks in advance!

A: 

I'm not sure how you can use Regular Expression in Replace With box in UltraEdit.

You can use this regular expression to find your string:

^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$
Vadim
+1  A: 

If your input is a single IP address (per line) and nothing else (no other text), this approach will work:

I used "Replace All" with Perl style regular expressions:

Replace (?<!\d)(?=\d\d?(?=[.\s]|$))
with    0

Just replace as often as it matches. If there is other text, things will get more complicated. Maybe the "Search in Column" option is helpful here, in case you are dealing with CSV.

Tomalak
I tried it with Ultra-Edit v.14.20 and it didn't work for me. Any idea why?
Vadim
Have you switched to "Perl" regular expressions before trying it?
Tomalak
+1  A: 

If this is just a one-off data cleaning job, I often just use Excel or OpenOffice Calc for this type of thing:

  1. Open your textfile and make sure only one IP address per line.
  2. Open Excel or whatever and goto "Data|Import External Data" and import your textfile using "." as the separator.
  3. You should now have 4 columns in excel:

    192 | 168 | 1 | 1

  4. Right click and format each column as a number with 3 digits and leading zeroes.

  5. In column 5 just do a string concatenation of the previous columns with a "." in between each column: A1 & "." & B1 & "." & C1 & "." & D1

This obviously is a cheap and dirty fix and is not a programmatic way of dealing with this, but I find this sort of technique useful for cleaning up data every now and then.

Seanchán
Hehe that's exactly what I've done before I opened this question thread ;)
Underlines
+3  A: 

Set the regular expression engine to Perl (on the advanced section) and replace this:

(?<!\d)(\d\d?)(?!\d)

with this:

0$1

twice. That should do it.

dr Hannibal Lecter