[wplug] Regex help?

Eric Wood esw148 at cawunited.com
Mon Apr 27 17:15:05 EDT 2015


@Michael, you're missing a grouping.
Also, it may be version-dependent, but I had to escape my () and {} in the
version of Emacs I'm using.

So Doug, here are a couple of options with that in mind:

Assuming:
You will have between 1 and 4 digits (will not match lines that start with
5 or more digits/periods)
Each digit is followed by a period
Then that string of digits and periods is followed by a space.


This should give you what you need:

^\(\([0-9]\.\)\([0-9]\.\)?\([0-9]\.\)?\([0-9]\.\)?\)[ ]

or simplified:

^\(\([0-9]\.\)\{1,4\}\)[ ]

String to use in the "replace with":

\1,     (include a space at the end after the comma if you want keep the
space in the end result)


That will convert the following:

7.1.3.2. Section describing general attributes of an item.
7. Section describing general attributes of an item.
7.1.3. Section describing general attributes of an item.

Into:

7.1.3.2., Section describing general attributes of an item.
7., Section describing general attributes of an item.
7.1.3., Section describing general attributes of an item.

It will do nothing to these lines:

7.1.3.2.6. Section describing general attributes of an item.
7.1.3.2.Section describing general attributes of an item.

This was tested successfully on GNU Emacs 21.4.1

Thanks,
Eric



On Mon, Apr 27, 2015 at 4:52 PM, Michael Skowvron <
michaels at penguincentral.org> wrote:

> This may also do what you need
>
> perl -nle '($numbers, $text) = $_ =~ /^([\d\.]+) (.*)$/; print $numbers,",
> ",$text'  < infile  > outfile
>
>
>
> On Mon, Apr 27, 2015 at 3:35 PM, Chris Clonch <cclonch at gmail.com> wrote:
>
> > I've had good luck with http://www.regexr.com/ for testing regex's.
> >
> > /([0-9]*)[0-9]\./ will match any series of numbers ending in a period.
> >
> > -Chris
> >
> > On Mon, Apr 27, 2015 at 3:01 PM, Doug Green <diego96 at mac.com> wrote:
> >
> > > Hi all,
> > > I've got some work data that I'm trying to clean up. It's a single
> column
> > > of Excel data, each row contains a chapter/section/subsection/paragraph
> > > number followed by a string of text. I'd like to separate the heading
> > > numbers into a different column than the body of the text (creating a
> two
> > > column data set).
> > >
> > > My plan was to do a CSV export, then add a comma after the number. By
> > > re-importing the CSV this <should> put the section number in a
> different
> > > column than the text. Example:
> > >
> > > 7.1.3.2. Section describing general attributes of an item.
> > >
> > > Converted to:
> > >
> > > 7.1.3.2., Section describing...
> > >
> > > Emacs has a convenient replace-regex function that simply asks first
> what
> > > regex you want to search for and next what you want to replace it with.
> > > I've tried every combo of "[0-9]\." and "[:digit:]\." but I'm not
> > matching
> > > ANY results. Can anyone point me in the right direction for a little
> help
> > > on writing a generalized regex that will match "any number followed by
> a
> > > period, optionally followed by up to 3 more numbers each followed by
> > > periods"? Clear as mud, right?
> > >
> > > Thanks!
> > > _______________________________________________
> > > wplug mailing list
> > > wplug at wplug.org
> > > http://www.wplug.org/mailman/listinfo/wplug
> > >
> > _______________________________________________
> > wplug mailing list
> > wplug at wplug.org
> > http://www.wplug.org/mailman/listinfo/wplug
> >
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug
>


More information about the wplug mailing list