Format all IP-Addresses to 3 digits - regex

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!

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.

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.

If this is just a one-off data cleaning job, I often just use Excel or OpenOffice Calc for this type of thing:
Open your textfile and make sure only one IP address per line.
Open Excel or whatever and goto "Data|Import External Data" and import your textfile using "." as the separator.
You should now have 4 columns in excel:
192 | 168 | 1 | 1
Right click and format each column as a number with 3 digits and leading zeroes.
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.

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])$

Related

Using a regex to increment a number [duplicate]

I've to add numbers incrementally in the beginning of every line using Notepad++.
It is the not the very beginning. But, like
when ID = '1' then data
when ID = '2' then data
when ID = '3' then data
.
.
.
.
when ID = '700' then
Is there any way i can increment these numbers by replacing with any expression or is there any inbuilt-notepad functions to do so.
Thanks
If you want to do this with notepad++ you can do it in the following way.
First you can write all the 700 lines with template text (you can use a Macro or use the Edit -> Column Editor). Once you have written it, put the cursor on the place you want the number, click Shift+Alt and select all the lines:
It's not possible to accomplish this with a regular expression, as you will need to have a counter and make arithmetic operations (such as incrementing by one).
You can try the cc.p command of ConyEdit. It is a cross-editor plugin for the text editors, of course including Notepad++.
With ConyEdit running, copy the text and the command line below, then paste:
when ID = '#1' then data
cc.p 700
Gif example

Notepad++ regular expression find a "," and replace odd "," with "." in every row

I ran into a mess recently with my data aquisition program which saves four datapoints separated by a comma(csv format) every couple of milliseconds. I used a PC (NL region) where the decimal point is a "," for data acquisition.
Now when i try to import my csv file to matlab/excel it gives me 8 columns (which should be 4) as all the decimals are also printed as ","
Is there a way to use regular expression in notepad++ (for eg) to find all "," in a row, and replace the odd ones to a "."?
Thanks a lot for any help. I have thousands of rows of data such that doing it manually will take ages.
Example raw data:
0,000000,293,625871,331,588659,37,440656
0,049000,294,148003,332,215504,37,400764
0,098000,294,814740,332,944775,37,261284
0,145000,295,683491,333,688803,37,184621
0,193000,296,504183,334,271264,37,058032
0,241000,297,213232,334,704293,37,109150
0,289000,297,595142,335,081749,37,113087
0,339000,297,968663,335,292896,37,088883
0,403000,298,204013,335,796915,37,109307
How the processed data should look:
0.000000,293.625871,331.588659,37.440656
0.049000,294.148003,332.215504,37.400764
0.098000,294.814740,332.944775,37.261284
0.145000,295.683491,333.688803,37.184621
0.193000,296.504183,334.271264,37.058032
0.241000,297.213232,334.704293,37.109150
0.289000,297.595142,335.081749,37.113087
0.339000,297.968663,335.292896,37.088883
0.403000,298.204013,335.796915,37.109307
Just simply do:
Find what: (\d+),(\d+)
Replace with: $1.$2
Then clic on Replace all
To match all odd commas, use a look ahead that asserts an even number of commas follow:
,(?=(([^,]*,){2})*[^,]*$)

Regular Expression: Replace values according to a translation table

How can I replace a list of values like
married
single
non
married
couple
to a list like this using a regular expression
Status 2
Status 1
non
Status 2
couple
? I know can match each group by something like this
/(married|single)/gm
and that I can address the matched group by $1, $2, ... . But how can I address and/or if-else the group-value in the replace-part to acutally translate the values?
Edit
Let's say I have the values to replace in a MariaDB-colum marital in myTable. Then I can do something like
SELECT
marital,
REGEXP_REPLACE(REGEXP_REPLACE(marital,
"married", "Status 2")
, "single", "Status 1")
FROM myTable
To get the desired result. But Is there a way to do this with just one REGEXP_REPLACE?
Thanks for your help!
You cannot do it with a single REGEXP_REPLACE because MariaDB doesn't support the required features in the third parameter.
You may do it using PHP with arrays: http://php.net/manual/en/function.preg-replace.php
or with callback: http://php.net/manual/en/function.preg-replace-callback.php
You may do it using Perl: How to replace a set of search/replace pairs?

Regular Expression Notepad increment numbers in every line

I've to add numbers incrementally in the beginning of every line using Notepad++.
It is the not the very beginning. But, like
when ID = '1' then data
when ID = '2' then data
when ID = '3' then data
.
.
.
.
when ID = '700' then
Is there any way i can increment these numbers by replacing with any expression or is there any inbuilt-notepad functions to do so.
Thanks
If you want to do this with notepad++ you can do it in the following way.
First you can write all the 700 lines with template text (you can use a Macro or use the Edit -> Column Editor). Once you have written it, put the cursor on the place you want the number, click Shift+Alt and select all the lines:
It's not possible to accomplish this with a regular expression, as you will need to have a counter and make arithmetic operations (such as incrementing by one).
You can try the cc.p command of ConyEdit. It is a cross-editor plugin for the text editors, of course including Notepad++.
With ConyEdit running, copy the text and the command line below, then paste:
when ID = '#1' then data
cc.p 700
Gif example

Regex to replace string with another string in MS Word?

Can anyone help me with a regex to turn:
filename_author
to
author_filename
I am using MS Word 2003 and am trying to do this with Word's Find-and-Replace. I've tried the use wildcards feature but haven't had any luck.
Am I only going to be able to do it programmatically?
Here is the regex:
([^_]*)_(.*)
And here is a C# example:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
String test = "filename_author";
String result = Regex.Replace(test, #"([^_]*)_(.*)", "$2_$1");
}
}
Here is a Python example:
from re import sub
test = "filename_author";
result = sub('([^_]*)_(.*)', r'\2_\1', test)
Edit: In order to do this in Microsoft Word using wildcards use this as a search string:
(<*>)_(<*>)
and replace with this:
\2_\1
Also, please see Add power to Word searches with regular expressions for an explanation of the syntax I have used above:
The asterisk (*) returns all the text in the word.
The less than and greater than symbols (< >) mark the start and end
of each word, respectively. They
ensure that the search returns a
single word.
The parentheses and the space between them divide the words into
distinct groups: (first word) (second
word). The parentheses also indicate
the order in which you want search to
evaluate each expression.
Here you go:
s/^([a-zA-Z]+)_([a-zA-Z]+)$/\2_\1/
Depending on the context, that might be a little greedy.
Search pattern:
([^_]+)_(.+)
Replacement pattern:
$2_$1
In .NET you could use ([^_]+)_([^_]+) as the regex and then $2_$1 as the substitution pattern, for this very specific type of case. If you need more than 2 parts it gets a lot more complicated.
Since you're in MS Word, you might try a non-programming approach. Highlight all of the text, select Table -> Convert -> Text to Table. Set the number of columns at 2. Choose Separate Text At, select the Other radio, and enter an _. That will give you a table. Switch the two columns. Then convert the table back to text using the _ again.
Or you could copy the whole thing to Excel, construct a formula to split and rejoin the text and then copy and paste that back to Word. Either would work.
In C# you could also do something like this.
string[] parts = "filename_author".Split('_');
return parts[1] + "_" + parts[0];
You asked about regex of course, but this might be a good alternative.