all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <embe8573@student.uu.se>
To: help-gnu-emacs@gnu.org
Subject: Re: regular expression
Date: Mon, 30 Jun 2014 22:04:39 +0200	[thread overview]
Message-ID: <871tu6b1uw.fsf@debian.uxu> (raw)
In-Reply-To: d5b8df44-60fd-4b8f-83d1-cb7d04b2a7b4@googlegroups.com

renato.pontefice@gmail.com writes:

> Hi, I'm newbe on this group.
>
> I know, that I can use Regexp in emacs. And, I would
> do that. Can someone help me?
>
> I have a text file, that is a converted .pdf
> file. So, I have many dirty character inside.
>
> I've found some reg- expression.
>
> i.e.: in a line like this:
>
> 40 STREET DW...
>
> I want to made substitution like this:
>
> 40#STREEDW...
>
> can someone help me to build this expression?

I suspect it is better to use gnu.emacs.help for this
kind of question as that group is much more
active. Therefore, I post this on both groups. You can
later remove the crosspost depending on where the
action is from now on.

As for your question, you only give one example so I
had to guess a bit what the general case is. For just
one example, you might as well use one (non-regexp)
search-and-replace, right? But I suspect you want to do
this on all cases like this:

40 STREET DW
6 ROAD EW
666 A Z
666 a z

So try the below command:

(replace-regexp
 "\\([0-9]+\\) \\([A-Z]+\\) \\([A-Z]+\\)"
 "\\1#\\2\\3")

Here is how it works:

[...] are ranges

+ is "one, or many (but never zero) of the previous"

whitespace is whitespace

\\(...\\) is a group - those are used in the "replace
with" expression - \\1 means insert group 1 (from left
to right), and so on.

Note that [A-Z] matches [a-z] as well (the lowercase
equivalent) unless the variable case-fold-search is
nil. If you want to have case-sensitive replacement
(where [A-Z] makes sense), you can enclose the command
like this:

(let ((case-fold-search nil))
  (replace-regexp
    "\\([0-9]+\\) \\([A-Z]+\\) \\([A-Z]+\\)"
    "\\1#\\2\\3") )

You can watch this in action by running it on the
examples above - see how now, the "a z" one is left
alone!

Yes, you can do this without writing code - but it is
easier to write it in code and execute it. The reason
is you have better overview and it is easier to adjust
the regexp (both the match and replacement parts) - and
this is often a thing you'd do a couple of times, to
get it right. So that is easier than to input it all
again and again interactively.

Come back with more question if you have any. Otherwise
tell us if you got it to work. Good luck!

-- 
underground experts united:
http://user.it.uu.se/~embe8573


       reply	other threads:[~2014-06-30 20:04 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <d5b8df44-60fd-4b8f-83d1-cb7d04b2a7b4@googlegroups.com>
2014-06-30 20:04 ` Emanuel Berg [this message]
2014-06-30 20:13   ` regular expression Emanuel Berg
2014-06-30 20:36     ` Teemu Likonen
     [not found]     ` <mailman.4605.1404160609.1147.help-gnu-emacs@gnu.org>
2014-06-30 20:52       ` Emanuel Berg
2014-06-30 21:04         ` Teemu Likonen
     [not found]         ` <mailman.4609.1404162300.1147.help-gnu-emacs@gnu.org>
2014-06-30 21:11           ` Emanuel Berg
2014-07-02 23:14 Tak Kunihiro
  -- strict thread matches above, loose matches on Subject: below --
2014-07-02 13:10 Tak Kunihiro
2014-07-02 13:43 ` Stefan Monnier
     [not found] <mailman.4622.1404173952.1147.help-gnu-emacs@gnu.org>
2014-07-02  8:33 ` Emanuel Berg
2014-06-30 23:14 Tak Kunihiro
2010-01-25 11:00 Burkhard Schultheis
2010-01-25 12:35 ` Nuno J. Silva
2010-01-25 12:53   ` Helmut Eller
2010-01-26 19:46     ` Nuno J. Silva
2010-01-25 16:34   ` Burkhard Schultheis
2010-01-25 18:12 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871tu6b1uw.fsf@debian.uxu \
    --to=embe8573@student.uu.se \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.