“Regex di Python” Kode Jawaban

Python Regex

import re

# Regex Cheat sheet : https://www.dataquest.io/blog/regex-cheatsheet/
# Regex python tester : https://pythex.org/
# re doc : https://docs.python.org/3/library/re.html

text = "i like train" 
reg = r"[a-c]" #the group of char a to c

if re.match(reg, text): #Check if regex is correct
	print(text)
else:
  print("Not any match")
Nervous Nightingale

Re Python3

import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'
Proud Penguin

Regex di Python

import re

text = "test1, test2, test3"
regex = re.compile(r"test1")

# Returns range of first match
print(regex.match(text).span())

# Returns text with all matches replaces with other text
print(regex.sub("replace", text))

# Returns every match
print(regex.findall(text))

# OUT:
#
# (0, 5)
# replace, replace, replace
# ['test1', 'test2', 'test3']
RyanGar46

Regex di Python

# Let's say you want to check for a phone number in a string
# Note: Remove indentation

	import re
	phone_num_regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
	mobile_string = 'My number is 415-555-4242' # Not real number
	any_phone_numbers = phone_num_regex.search(mobile_string)
	print(any_phone_numbers)

The r in front of the string means it's a raw string (/n, /t, etc doesn't work)
In regex, if we use \d, it will look for any digit in your string (0-9)

If we search for \d\d\d-\d\d\d-\d\d\d\d, it will look for anywhere in the
string where there is a digit, followed by a digit, followed by a digit, followed
by a hyphen, ...

You can also use it in an if statement to check if there is a match or not
between a regex and a string with 're.match(regex, string)'


Ninja Penguin

Python Regex

# Recursive Python3 program to find if a given pattern is
# present in a text
 
def exactMatch(text, pat, text_index, pat_index):
    if text_index == len(text) and pat_index != len(pat):
        return 0
  
    # Else If last character of pattern reaches
    if pat_index == len(pat):
        return 1
  
    if text[text_index] == pat[pat_index]:
        return exactMatch(text, pat, text_index+1, pat_index+1)
  
    return 0
 
  
# This function returns true if 'text' contain 'pat'
def contains(text, pat, text_index, pat_index):
    # If last character of text reaches
    if text_index == len(text):
        return 0
  
    # If current characters of pat and text match
    if text[text_index] == pat[pat_index]:
        if exactMatch(text, pat, text_index, pat_index):
            return 1
        else:
            return contains(text, pat, text_index+1, pat_index)
  
    # If current characters of pat and tex don't match
    return contains(text , pat, text_index+1, pat_index)
  
# Driver program to test the above function
 
print(contains("geeksforgeeks", "geeks", 0, 0))
print(contains("geeksforgeeks", "geeksquiz", 0, 0))
print(contains("geeksquizgeeksquiz", "quiz", 0, 0))
 
# This code is contributed by ankush_953.
samuel karanja

Cara Belajar Regex Pyton

re.search(r'cake$', "Cake! Let's eat cake").group()

## The next search will return the NONE value, try it:
re.search(r'cake$', "Let's get some cake on our way home!").group()
Enchanting Eagle

Jawaban yang mirip dengan “Regex di Python”

Pertanyaan yang mirip dengan “Regex di Python”

Lebih banyak jawaban terkait untuk “Regex di Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya