HomePagev2 Forums Python Discussions Regular Expressions

  • Regular Expressions

  • Karri Bindu Madhavi

    Member
    October 1, 2020 at 4:55 am

    Hello everyone,

    Can anyone explain about “Regular expressions”  briefly?

    thank you

  • Sai Gowtham Babu AMBURI Gowtham Babu AMBURI

    Member
    October 1, 2020 at 5:14 am

    Hello,

    A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.

    RegEx can be used to check if a string contains the specified search pattern.

    Python has a built-in package called re, which can be used to work with Regular Expressions.

    RegEx in Python

    When you have imported the re module, you can start using regular expressions:

    Example

    Search the string to see if it starts with “The” and ends with “Spain”:

    import re

    txt = "The rain in Spain"
    x = re.search("^The.*Spain$", txt)

    if x:
    print("YES! We have a match!")
    else:
    print("No match")

    you get a no match as output.

    Use a condition if you want to check weather the required elements are present or not in the string.

    There is lot more to know about Regular expressions and the different functions used in that.

    I would recommend you to go through the following link to know more expressions used

    https://www.w3schools.com/python/python_regex.asp

  • Sai Gowtham Babu AMBURI Gowtham Babu AMBURI

    Member
    October 1, 2020 at 5:20 am

    Sorry in the above condition you get yes! We have a match as output

  • Badri Rajasekhar Reddy Rajasekhar Reddy

    Member
    October 1, 2020 at 5:25 am

    Good explanation gowtham anna Thank you 🙂

  • S.L.T.Sowjanya

    Member
    October 1, 2020 at 7:46 am

    Thank you gowtham

  • Karri Bindu Madhavi

    Member
    October 2, 2020 at 1:34 am

    good explanation annaya,thank you annaya i understood

  • Sai Gowtham Babu AMBURI Gowtham Babu AMBURI

    Member
    October 5, 2020 at 4:59 pm

    Python has a built-in package called re, which can be used to work with Regular Expressions.

    When you have imported the re module, you can start using regular expressions:

    Example

    To see if it starts with “The” and ends with “Spain”:

    import re

    txt = "The rain in Spain"
    x = re.search("^The.*Spain$", txt)

    if x:
        print("YES! We have a match!")
    else:
        print("No match")
    the output is going to be YES! We have a match!

    since we have the characters present in the code

    RegEx Functions

    The re module offers a set of functions that allows us to search a string for a match:

    Function Description
    findall Returns a list containing all matches
    search Returns a Match objectif there is a match anywhere in the string
    split Returns a list where the string has been split at each match
    sub Replaces one or many matches with a string

    Metacharacters

    Metacharacters are characters with a special meaning:

    Character Description Example
    [] A set of characters “[a-m]”
    \ Signals a special sequence (can also be used to escape special characters) “\d”
    . Any character (except newline character) “he..o”
    ^ Starts with “^hello”
    $ Ends with “world$”
    * Zero or more occurrences “aix*”
    + One or more occurrences “aix+”
    {} Exactly the specified number of occurrences “al{2}”
    | Either or “falls|stays”
    () Capture and group

    I hope you understood about how regular expressions can be used. Try to use a condition so that you can know whether the elements are present in a given string or not.

    I have preferred to think link below to explain about RE. There are many other examples in the following Link below.
    It would be more helpful.
    Thank you .
  • Karri Bindu Madhavi

    Member
    October 6, 2020 at 11:01 am

    got it annaya,thanks for the information annaya

Log in to reply.

Original Post
0 of 0 posts June 2018
Now