Home > Mobile >  Replace or remove period/dot from expression
Replace or remove period/dot from expression

Time:03-15

I have this expression in PCRE and I want to leave/remove the . (period) out of klantnummer.

Expression:

^h:\/Klant\/(?<klantnummer>[^\/] )\/(?<folder1>[^\/] )\/(?<folder2>[^\/] )

Input:

h:/Klant/12345678.9/map 1/map 2

Outcome: 12345678.9

Desired result: 123456789

https://regex101.com/r/EVv47V/1

So Klantnummer should have 123456789 as result

CodePudding user response:

You can't do that in one step. You could catch it in two Capture Groups:

^h:\/Klant\/(?<klantnummer1>[^\.\/] )\.(?<klantnummer2>[^\/] )\/(?<folder1>[^\/] )\/(?<folder2>[^\/] )

and put both together by string concatenation after or use two regex steps and filter out the period in the second, like stated in comments.

Regex above assumes there is always a period, this will work for 0 or 1 period in the number:

h:\/Klant\/(?<klantnummer1>[^\.\/] )(?:\.?(?<klantnummer2>[^\.\/] ))\/(?<folder1>[^\/] )\/(?<folder2>[^\/] )

CodePudding user response:

As already discussed you can't do this on one step.
The solution of using 2 regex stages, or 2 splitting klantnummer into 2 groups before and after the capture group will both work.
However I believe that the simplest and most efficient both in terms of computer power and of code to write, will be to replace .with and empty String '' after the regex, and before using it.
You haven't said which programming language you are using so I can't give you the syntax/example.
If all that you are doing is splitting the String on the slashes you will probably find it easier to split the string into an array.
For example in python

s = "h:/Klant/12345678.9/map 1/map 2"
array = s.split('/')
Klantnummer=array[2].replace('.','')
folder1=array[3]
folder2=array[4]
print(Klantnummer)
print(folder1)
print(folder2)

output

123456789
map 1
map 2

Tested on https://www.online-python.com/

  • Related