cancel
Showing results for 
Search instead for 
Did you mean: 

Regex any one?

Community Veteran
Posts: 6,584
Thanks: 206
Fixes: 14
Registered: 16-02-2009

Regex any one?

I am trying to replace each instance of a 1 in a text file, but not if it is used in a number.
The text files I have have several 1's dotted though the text like1 th1is 1example. 19:00.
I have thought that a regex replacement would work, but can't figure out the correct one.
\d finds all the numbers, [a-zA-Z]1[a-zA-Z] doesn't work...
So do I keep doing it manually or does someone know a regex replace that will work?
5 REPLIES
kmilburn
Grafter
Posts: 902
Thanks: 2
Registered: 30-07-2007

Re: Regex any one?

Matching on a-z should work with 'th1s',  but wouldn't with '1example', unless you add spaces too,  e.g. [ a-zA-Z]1[ a-zA-Z]
Alternatively you could try excluding 0-9, which also caters for puntuaction and line begin/end.  e.g. [^\d]1[^\d]
The only snag with both of these,  is that they will both select lonesome 1's
You probably also want to tag  the before and after match so it can be replaced correctly.   ([^\d])1([^\d])

The behaviour may depend on what tool you're using,  these expressions worked in Programmers Notepad
Community Veteran
Posts: 767
Registered: 29-10-2008

Re: Regex any one?

Late reply, but if you just want to remove 1s from words, I think you can do a before OR after, and sub whichever, so
$ echo "1fo1o1 ba11r 12 1 01:30 " | sed -r 's/(([[:alpha:]])1|1([[:alpha:]]))/\2\3/g'
foo bar 12 1 01:30

But where did all these 1s come from?
Gabe
Community Veteran
Posts: 6,584
Thanks: 206
Fixes: 14
Registered: 16-02-2009

Re: Regex any one?

They are scanned documents, and I believe they are glitches, they occur inside the words so unlikely to be page numbers.
Nice sed line, but the editor I am using (they are HTML/epubs, and I am using Sigil) doesn't allow sed, and it would pick up all the formatting <h1> etc, if I was to try it from the command line.
I will give the others a try.
Community Veteran
Posts: 767
Registered: 29-10-2008

Re: Regex any one?

Don't have to use sed; that's just a quick way of testing the regex. The same regex will work as Find-Replace, but if your text is tagged then either that or the above will zap your tags. I guess in Sigil you could use something like
Find: (([[:alpha:]])1|1([[:alpha:]]))(?![^<>]*>)
Replace: \2\3

to keep the Replace out of the tags. Hmm.
Not sure this is chit chat
cat tesco | sed -r 's/(beef|pork|lamb|duck|pope)/horse/g' >halogenoven

Crazy
Gabe
Community Veteran
Posts: 6,584
Thanks: 206
Fixes: 14
Registered: 16-02-2009

Re: Regex any one?

Thanks that worked like a dream