Home > database >  Ignore only one special characters in regex
Ignore only one special characters in regex

Time:02-16

this is a general question to write a regular expression to exclude only one special characters. Let's take this example: Note that the question is not related to bullet point.

regx = r'(?<=\*\)\s)[\w\s,\.] '

re.findall(rr, '*) Do you like, pizza, salmon and fruits? *) Yes, I like Pizza and fruits, but I dont like salmon')

['Do you like, pizza, salmon and fruits? ',
 'Yes, I like Pizza and fruits, but I dont like salmon']

Here, I have to specify ,, . and ? to match correctly. In general, we can get other special characters. So, I want to include all the characters and exclude only *. I try this, but it does not work:

regx = r'(?<=\*\)\s)[\w\W^*] '

CodePudding user response:

Have you tried it this way? I think all characters except * can also be specified like this [^*]. See below:

Using regex to match any character except =

CodePudding user response:

I would phrase your problem as trying to find each bulleted item following *):

inp = '*) Do you like, pizza, salmon and fruits? *) Yes, I like Pizza and fruits, but I dont like salmon'
items = re.findall(r'\*\)\s*(.*?)(?=\s*\*\)|$)', inp)
print(items)

This prints:

['Do you like, pizza, salmon and fruits?',
 'Yes, I like Pizza and fruits, but I dont like salmon']

CodePudding user response:

I would phrase your problem as trying to find each bulleted item following *):

inp = ') Do you like, pizza, salmon and fruits? ) Yes, I like Pizza and fruits, but I dont like salmon' items = re.findall(r'*)\s(.?)(?=\s**)|$)', inp) print(items)

  • Related