I need a C# regex to extract the last 6 letters of a string and add a forward slash in the middle of them. For example:
xx_xxxx_xxxABCXYZ.Something.csv
I need ABC/XYZ
The 3 letter components could be anything and the amount of x’s in front varies. However, the 6 letters I need are always immediately preceding the first period.
I don’t have the option of additional code because I need to input the Pattern and Replacement into a GUI.
Much appreciated.
Use this:
Replace with:
The pattern matches the whole string so everything gets removed in the end, but it captures the two string of 3 characters each in capturing groups. These are then accessed and put back in place with
$1and$2. The pattern is also followed by\., so there has to be a dot following the six characters. The?after the.*makes sure that you find the first possible occurrence (instead ofething).Note that this allows for digits, letters and underscores to make up the six characters. If you only want letters, use
[a-zA-Z]or[A-Z]for upper-case only.