2

I have a text like this:

''Lorem ipsum'' dolor ''sit amet, '' consectetur adipiscing elit.

What I want is to get the sit amet correctly wrapped around with '', like this:

''Lorem ipsum'' dolor ''sit amet'', consectetur adipiscing elit.

The problem is that my regular expression selects this:

''Lorem ipsum'' dolor ''sit amet, ''

instead of just this:

''sit amet, ''

and, if that is the case, the text selected is not the text I want to correct. Any help?

EDIT

Sorry, yes, indeed I didn't provided any regexp. The thing is I'm using visual-regexp and visual-regexp-steroids, in other words I'm using the regular expression engine(?) that comes with Python. That's why I didn't provided any.

The regexp ''([^']+), '' works ok (I still have to check the entire 11MB of text) as suggested by mbork.

  • 1
    You didn't help us a lot by not providing your regex. But: is this what you are looking for: gnu.org/software/emacs/manual/html_node/emacs/… ? – mbork Dec 28 '14 at 22:20
  • 2
    Ah. If it's something like ''\(.+\), '', then non-greedy operators won't help apparently. Maybe try something like ''\([^']+\), ''? – mbork Dec 28 '14 at 22:21
  • 2
    @mbork This will fail for ''sit 'amet, '', won't it? – T. Verron Dec 28 '14 at 23:35
  • 2
    Yes it will, of course. But it might be true that the OP doesn't have any ticks in his text, no? In general: I'm not sure regexen are really the best way to do what the OP wants to do. Another idea would be first to convert all '' sequences to some single character not appearing anywhere in the buffer, then do the transformation, and then convert back. Or just write some Elisp to do what the OP needs. Currently, the problem is underspecified anyway. – mbork Dec 29 '14 at 0:51
  • 1
    Also, I'd consider (non-regex) replacing , '' with '',, possibly interactively (in case one gets false positives). – mbork Dec 29 '14 at 0:53
3

Non-greedy *? or +? postfix operators are what I usually use in these situations. In Emacs read syntax:

"''\\(.*?\\)''"

will match anything enclosed in '' which does not contain '' (because if it contained it, there would be a shorter (less-greedy) match).

n.b.: . does not match newlines in Emacs regexps. To also allow for newlines between the quotes, you could use:

"''\\(\\(?:.\\|\n\\)*?\\)''"

In either case, \1 is grouping the contents between the '' quotes.

0

A classical trick is to use (re-search-forward "''\\([^']\\|'[^']\\)+, ''"). The group means "no tick, or only one".

-1

The solution given by mbork worked fine. I had other case like ''sit amet '' where using \([^']+\) wasn't a good idea. I decided then to build this regexp '+(\w+) '+ (using the Python re module through visual-regexp-steroids, an Emacs extension) and my text is almost done!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.