2

I'm using TeXworks (Version 0.4.5) and came in touch with its regular expression (regex) feature, recently. I made some adjustments of the syntax-highlighting, for instance, to practice.

Now I'm asking: Is it possible that TeXworks doesn't support the lookbehind (?<= ) expression? At least in the Find&Replace dialogue? Because my regex-input gets marked "invalid" as soon as I try to use it there. Also, the TeXworks-manual doesn't list the lookbehind-, but it does list the lookahead-expression.

The actual problem I want to solve: I want to replace \verb+BLABLA+ by \emph{BLABLA}. I tried to search for \+(?<=verb\+.*) and replace the match (which should be the closing +) by }. Then I'd search for verb\+ and replace that by emph{.

I stumbled over the fact, that most regex-flavors don't support the .* or '.+' in a loobehind, though, which still leaves the question above unanswered.

Secondly, how would you solve the actual problem if not via lookbehind?

  • I've never heard of this feature though it definitely sounds interesting. In Kile, I would use something like \\verb\+([^\+]*)\+ for find and \\emph\{\1\} for replace. Obviously that might not be quite right for TeX Works. For sed I would use something like s/\\verb\+\([^\+]*\)\+/\\emph{\1}/g'. – cfr Apr 12 '14 at 21:54
  • Regular expressions can only do certainly things: more powerful highlighting requires other approaches. That's I think a 'known limitation' of TeXworks: since it uses regex highlighting it can't match some other editors in terms of picking up non-regular input syntaxes ((La)TeX isn't regular). – Joseph Wright Apr 13 '14 at 1:38
  • @cfr TeXworks uses regex for syntax-highlighting (and for auto-completion?) and one can activate it to be used in find&replace as well. Your "\1-method" uses back references, right? – LCsa Apr 13 '14 at 9:42
  • @cfr This one works!! Square brackets indicate a set that has to be (in this case not because of the ^) matched, right? @JosephWright I appearently don't know what regular expressions (and non-regular ones) exactly are... – LCsa Apr 13 '14 at 9:49
  • 1
    @cfr Well possibly borderline but as TeXworks does have regex searching and is TeX-focussed it's not unreasonable to ask it here. – Joseph Wright Sep 7 '14 at 15:28
3

I've never heard of this feature though it definitely sounds interesting. I would just use regular expressions.

Implementations of regular expressions vary. Since I'm not familiar with TeXworks, I cannot say exactly which syntax to use but I'll give two examples.

In Kile, I would use something like this for find:

\\verb\+([^\+]*)\+ 

and for replace:

\\emph\{\1\}

For sed I would use something like

s/\\verb\+\([^\+]*\)\+/\\emph{\1}/g

So I'd write something like

sed 's/\\verb\+\([^\+]*\)\+/\\emph{\1}/g' <filename.tex> > <filename-new.tex>

or else I would use

sed 's/\\verb\+\([^\+]*\)\+/\\emph{\1}/g' <filename.tex>

check the output and then use

sed -i 's/\\verb\+\([^\+]*\)\+/\\emph{\1}/g' <filename.tex>

to do the substitution in place. (Don't use -i without checking the output first, though. Unlike your editor, the command line has no 'undo' unless you use version control!)

The () in Kile and \(\) in sed are grouping and saving part of the pattern. This is then reused in the replacement as \1 which refers back to the corresponding part of the string being replaced.

\ escapes what would otherwise be interpreted as a special character rather than part of the pattern. So \\verb, \\emph and \+ are required for \verb, \emph and +.

[] matches any one of the characters specified. For example [abc] would match any of a, b or c. The ^ inside negates this. So [^\+] matches any character other than +.

* means zero or more of something. So a*b would match b, ab, aab and so on. Hence, [^\+]* matches any string of zero or more characters which does not include +.

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.