-4

I'm at a loss because I don't know how to write regular expressions of python to extract particular strings such as A =BC= D =EF= -> 'BC', 'EF. I searched a lot but couldn't write this operation. please help.

  • 1
    This isn't very well defined. Could you be more clear bout exactly what you are looking for this regex to do? – Patrick Haugh Aug 01 '18 at 16:55
  • thansk for helpful answers! please ask one more question! how about extracting strings from only left-side equal mark not just both side such as `A =BC= D =EF= =G H` -> `'BC', 'EF', 'G'` ? – user3620568 Aug 01 '18 at 17:30

3 Answers3

0

Something like this

=..=

regex.101

result:

Match 1

Full match 2-6 =BC=

Match 2

Full match 9-13 =EF=

Here is a nice tutorial: Regex tutorial — A quick cheatsheet by examples

Any Moose
  • 723
  • 6
  • 12
0

You could use =([^=]+)= to extract character (except =) any (non-zero) number of times. You can extract the contents within the equal signs using groups.

If you want to match exactly two characters within equal signs, =([^=]{2})= should do.

shreyasminocha
  • 550
  • 3
  • 14
  • thanks a lot! very helpful answer! please ask one more question. How about only left-side equal `A =BC= D =EF= =G H` -> `'BC', 'EF', 'G'` ? – user3620568 Aug 01 '18 at 17:24
  • @user3620568 Ideally you should edit your question to include this follow-up or ask a separate question, but try `=([^=]+)(?:=| )`. The only issue with that is that it includes the space after `G` in the capture. So you'll get `'BC', 'EF', 'G '`. – shreyasminocha Aug 01 '18 at 17:30
  • 1
    thanks for your answer! i will be careful next time. – user3620568 Aug 01 '18 at 17:34
  • @user3620568 If this answers your question adequately, please mark as accepted. No problem! – shreyasminocha Aug 01 '18 at 17:39
0

First you'll need to use the Regex library

import re

Then you can use re.findall(pattern, string) to get a list of all the substrings that match your pattern.

It's not clear from your question what defines the 'particular strings' you are looking for. Assuming you are looking for everything between two equals signs, but not greedily (not including equals signs inside), you could use the regex "=(.*?)=".

import re
m = re.findall("=(.*?)=", "A =BC= D =EF=")

Result:

>>>m

['BC', 'EF']

Community
  • 1
  • 1
UnderSampled
  • 131
  • 6