Python Forum

Full Version: What package to install to PyCharm... REGEX or RE?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am still very new to Python, and I want to write a Python program using Regular Expressions.

Now, I am a bit confused as to what Python package to install in PyCharm.

Since Python has a built-in package called re, which can be used to work with Regular Expressions, do I still need to install the REGEX package from PyPi.org?

Then, if I do need the REGEX package, what should I import before proceeding?:

import re or import regex?

Do they mean the same thing please?

Please help me out.

Thanks in anticipation.
(May-23-2020, 12:16 PM)charlesauspicks Wrote: [ -> ]Do they mean the same thing please?
re is in the Python Standard Library
regex is a 3-party module,that why is on PyPi.
regex Wrote:This regex implementation is backwards-compatible with the standard re module,
but offers additional functionality.
So it offers additional functionality,but just using re work for most use cases.
>>> import re
>>> 
>>> s = 'cat and dog'
>>> re.findall(r'dog', s)
['dog']
>>> re.findall(r'dog|cat', s)
['cat', 'dog']
(May-23-2020, 12:30 PM)snippsat Wrote: [ -> ]import re

So, that means importing the re module after installing REGEX from PyPi will give me access to the functions in the REGEX package, is that correct?

Or I still need to import regex separately before I can use the functions contained therein?
(May-23-2020, 12:48 PM)charlesauspicks Wrote: [ -> ]Or I still need to import regex separately before I can use the functions contained therein?
Have import regex to use it's additional functionality.
So it work the same as example over,but has add on stuff like eg expandf.
>>> import regex
>>>
>>> s = 'cat and dog'
>>> regex.findall(r'dog', s)
['dog']
>>> regex.findall(r'dog|cat', s)
['cat', 'dog']
>>>
>>> # Using add on <expandf>
>>> m = regex.match(r"(\w)+", s)
>>> m.expandf("{0} {1}")
'cat t'
Understood... Thanks doc!