-2

My question is quite simple I'm trying to come up with a RE to select any set of words or statement in between two characters.
For example is the strings are something like this :

') as whatever ' 

and it can also look like

') as whatever\r\n'

So i need to extract 'whatever' from this string.

The Regex I came up with is this :

\)\sas\s(.*?)\s

It works fine and extracts 'whatever' but this will only work for the first example not the second. What should i do in case of the second statement

I'm basically looking for an OR condition kind of thing!

Any help would be appreciated

Thanks in advance

Chandra Kanth
  • 427
  • 5
  • 14

2 Answers2

1

It is working as you intended. Please check it

import re
a =') as whatever '
b=') as whatever\r\n'
print re.findall(r'\)\sas\s(.*?)\s', a)[0]
print re.findall(r'\)\sas\s(.*?)\s', b)[0]

This will output as

'whatever'
'whatever'
Dixon MD
  • 168
  • 2
  • 11
1

The question is not very clear but maybe the regular expression syntax you are looking for might be something like this:

\)\sas\s(.*?)[\s | \r | \n]

basically telling after the string you are interested you can find a space or other characters.

EDIT As example take the following code in Python2. The OR operator is '|' and I used it in the square brackets to catch the strings which have as subsequent character a space, '\r' a . or 'd'.

import re
a = ') as whatever '
b = ') as whatever\r\n'
c = ') as whatever.'
d = ') as whateverd'
a_res =  re.findall(r'\)\sas\s(.*?)[\s | \r | \n]', a)[0] #ending with     space, \r or new line char
b_res =  re.findall(r'\)\sas\s(.*?)[\s | \r | \n]', b)[0]
c_res =  re.findall(r'\)\sas\s(.*?)[\s | \r | \on | \.]', c)[0] #ending with space, \r new line char or .
d_res =  re.findall(r'\)\sas\s(.*?)[\s | \r | \on | \. | d]', d)[0] #ending with space, \r, new line char, . or d
print(a_res, len(a_res))
print(b_res, len(b_res))
print(c_res, len(c_res))
print(d_res, len(d_res))
roschach
  • 8,390
  • 14
  • 74
  • 124
  • I updated the question with an example. It works in python2. If it does not work please copy the error. – roschach Feb 21 '18 at 15:55