Lucas Almeida Carotta
Lucas Almeida Carotta

Reputation: 567

Regex match any setence containing ^ except those that start with \

I want to add a delimiter -- $ -- to each sentence that contains an exponential -- ^ -- operator in my text, but I don't want to add this same delimiter when the base of my exponential contains a backslash in it. I tried to use the ((?!\\\w+)\w+)\^(\w+) regex to no success.

input what I want what I got
foo^2 $foo^2$ $foo^2$
\foo^2 \foo^2 $foo^2$

My code:

from re import sub

sentence = """foo^2 
 \bar, \bar^3 
bar 
\foo 
 \bar
 foo^5 
"""

sub(r'((?!\\\w+)\w+)\^(\w+)', '$\\1^\\2$', sentence)

Gives the following output:

'$foo^2$ \n \x08ar, \x08$ar^3$ \nbar \n\x0coo \n \x08ar\n $foo^5$ \n'

Upvotes: 0

Views: 43

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

You can use

import re
texts = [r'foo^2', r'\foo^2']
for text in texts:
    print(re.sub(r'(?<!\\)((?:\\\\)*\b\w+)\^(\w+)', r'$\1^\2$', text))

See the Python demo.

Output:
$foo^2$
\foo^2

See the regex demo. Details:

  • (?<!\\) - a location not immediately preceded with aa \ char
  • ((?:\\\\)*\b\w+) - Group 1: zero or more \\ string occurrences, a word boundary and one or more word chars
  • \^ - a ^ char
  • (\w+) - Group 2: one or more word chars.

Upvotes: 2

Rutangaba
Rutangaba

Reputation: 439

@Lucas Almeida Carotta, is that what you want?

sentence = r"""foo^2 
 \bar, \bar^3 
bar 
\foo 
 \bar
 foo^5 
"""

new_words = []

new_words = []

for word in sentence.split():
    if (re.search('^\\\\', word)):
        new_words.append(word)
        #print(word.replace('\\',''))
    else:
        #print('$'+word+'$')
        new_words.append('$'+word+'$')

OUTPUT:

print(new_words)

['$foo^2$', '\\bar,', '\\bar^3', '$bar$', '\\foo', '\\bar', '$foo^5$']

Upvotes: 0

Related Questions