Find/Replace Regex?
-
I have a lengthy text file containing expressions of the form “ABC123456” (three capital letters, 6 numbers). I need to add a “;” before and after the expressions.
The search term \D\D\D\d\d\d\d\d\d finds all expressions I need, but what term do I need for the replacement? -
@Jörg-Winkel said in Find/Replace Regex?:
The search term \D\D\D\d\d\d\d\d\d finds all expressions I need, but what term do I need for the replacement?
When you use a search term such as yours and you want to write it back in the replacement you can type
$0
which is all characters selected. So for putting a semi-comma either side the replacement could be;$0;
.However it would seem you only have a rudimentary knowledge or regular expressions as you may not be aware that
\D
means “not a digit” so it will select something starting with!#A
for example.I could go on to explain in more detail but I really think you need to learn from the ground up. We have some excellent reference links under the FAQ section on this forum. I would strongly suggest you start learning.
Terry
PS a cheat sheet is here
-
@Terry-R
You are correct, I have less than rudimentary knowledge about regular expressions. The last time I used them was more than 25 years ago.
It does, however, do exactly what I need today, so thanks a lot for your help. -
@Jörg-Winkel said in Find/Replace Regex?:
It does, however, do exactly what I need today, so thanks a lot for your help.
I thought I might follow up with some more information as I don’t think you realise how powerful/loose the use of the
\D
actually is. This post is also for others hoping to learn something.\D
means “not a digit” which sounds okay until you consider that it really means ANY character that isn’t a digit. So alpha’s are certainly part of that group as the OP wanted. However it also includes punctuation characters such as;:"',.?
and if you see my image, it will include line feed and/or carriage return characters. So ANY character except for the 10 digit ones.
The example in the image is admittedly made up on purpose to highlight the power of the\D
but it shows how a regular expression (regex) can select other strings in error if one isn’t aware of what it means. Also note that I included a 3 non-digit plus 7 digit string in the example. Again the regex selected the portion it was required to find, that of the 6 digits following the 3 non-digit string. So the 7th digit was excluded. This means upon editing the string we finish up with;#AB234567;8
. I would say that’s a definite error. If only 6 digit strings were to be selected, a test at the end of the find expression would look for 1 or more digits and based on the criteria, either exclude that string or include all of it.I’d say this is a prime example of a loosely made expression very likely to have unwarranted side effects.
Those who do create regexes for others take this into account and is often what we spend more time on, the “edge cases”. Edge cases can be likened to programming where 20% of the code does the work and 80% of the code is error checking. The percentages do vary according to who you talk to but the sentiment still exists across the programming world.
Terry