I have a regex in my django code but I don't know what it means actually. Here is my regex :
r'^email/(?P<email>[^#\s]+#[^#\s]+\.[^#\s]+)/$',
Could you give me some examples which match with this regex?
RegEx Circuit
You can visualize your expressions in jex.im:
You can also test/modify/change your expressions in regex101.com.
Basically, your expression would match:
email/some_alphanumeric[A-Z0-9]_special_chars_##$*some_alphanumeric_special_chars_#$*.some_alphanumeric_special_chars_#$*
Demo
If you wish to match:
myurl/email/blabla#blabla.com
You can modify it to:
myurl\/email\/([^#\s]+#[^#\s]+\.[^#\s]+)
Python Test
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"myurl\/email\/([^#\s]+#[^#\s]+\.[^#\s]+)"
test_str = "myurl/email/blabla#blabla.com"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
in addition :
r'^email/(?P<email>[^#\s]+#[^#\s]+\.[^#\s]+)/$'
this regx use in django url
url example : email/test#gmail.com/
email/ = consolent value in your url
[^#\s] = you can write any character except # and space "/s"
#[^#\s] = you must start with # + anything expect #character and space "/s"
\. = matches "."
[^#\s] = you can write anycharacter except # and space "/s"
+ = you can type many character
/$ = end of url
Related
This question already has answers here:
re.findall not returning full match?
(6 answers)
Closed 12 months ago.
I'm trying to reconstruct an example string from a given regular expression
test_re = r'\s([0-9A-Z]+\w*)\s+\S*[Aa]lloy\s'
However, the code below only gives ['1AZabc']
import re
txt = " 1AZabc sdfsdfAlloy "
test_re = r'\s([0-9A-Z]+\w*)\s+\S*[Aa]lloy\s'
# test_re = r'\s+\S*[Aa]lloy\s'
x = re.findall(test_re,txt)
print(x)
Why the contents after the space (for matching the \s+) is not captured by re? What is a simple and valid example string that matches the text_re?
Your code works and finds all - you just misunderstand regex GROUPs and its usage when calling findall:
# code partially generated by regex101.com to demonstrate the issue
# see https://regex101.com/r/Gngy0r/1
import re
regex = r"\s([0-9A-Z]+\w*)\s+\S*?[Aa]lloy\s"
test_str = " 1AZabc sdfsdfAlloy "
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# use findall and print its results
print(re.findall(regex, test_str))
Output:
# full match that you got
Match 1 was found at 0-20: 1AZabc sdfsdfAlloy
# and what was captured
Group 1 found at 1-7: 1AZabc
# findall only gives you the groups ...
['1AZabc']
Either remove the ( ) or put all into () that you are interested in:
regex = r"\s([0-9A-Z]+\w*\s+\S*?[Aa]lloy)\s"
I have a problem of the following nature. After reading the data from the csv file to the pandas DataFrame, I have the date in the first column. The format of this date is six characters "yymmdd" (int64). Unfortunately, all attempts to convert to the "yyyy-mm-dd" format have failed.
From the input "171207" it gets the value "1970-01-01 00: 00: 00.000171207". None of the functions I tested support the YY-MM-DD format. (Python 3.9) Asking for suggestions.
Thanks in advance!
You can handle the Problem by Hand.
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\d{2})(\d{2})(\d{2})"
test_str = "171223"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
For more info of Regex use http://regex101.com
This question already has answers here:
How to use regex to find all overlapping matches
(5 answers)
Closed 3 years ago.
I am trying to get all the substring that matches some delimiters. My issue is that i also need the character at the end of the last occurrence. The strings need to be between any of these characters: . , / , ? , = , - , _
I have tried this regular expression
pattern = re.compile(r"""[./?=\-_][^./?=\-_]+[./?=\-_]""")
In this exemple:
-facebook=chat.messenger?
I am not able to get the substring =chat.
I am only getting -facebook= and .messenger?
Looks like the overlap is what's causing some the drama. If using the regex module (which is expected to eventually replace the re module), you can do
import regex as re
delimiters = r'[./?=\-_]'
pattern = delimiters + r'[a-z]+' + delimiters
s = '-facebook=chat.messenger?'
print(regex.findall(pattern, s, overlapped=True))
# ['-facebook=', '=chat.', '.messenger?']
Notice that this assumes all characters are lowercase with [a-z], and that [./?=\-_] is the list of delimiters you specified.
Hope this helps!
My guess is that this expression might be what we might want to start with:
((?:[/?=_–.-])([a-z]+)(?:[/?=_–.-]))|([a-z]+)
Demo
Test
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"((?:[/?=_–.-])([a-z]+)(?:[/?=_–.-]))|([a-z]+)"
test_str = "-facebook=chat.messenger?"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
I'm trying to check str that is only Thai character or not by using regex or any if it can solve
I'm trying to use
re.compile(u"[^\u0E00-\u0E7F']|^'|'$|''")
ret = regexp_thai.sub("", s)
to slice another language or digit
by the way it just only slice not for return boolean
I expect output like
s = "engภาษาไทยที่มีสระ123!#"
regexp_thai = re.compile(u"[^\u0E00-\u0E7F']|^'|'$|''")
ret = regexp_thai.sub("", s)
print(ret) # ภาษาไทยที่มีสระ
print(isthai(ret)) # True
u0E00-u0E7F is a unicode of Thai language
How can I write isthai function
I'm not quite sure what might be the desired output. However, I'm guessing that we like to capture the Tai letters, which based on your original expression, we might just want to add a simple list of chars, wrap it with a capturing group and swipe our desired Tai letters from left to right, maybe similar to:
([\u0E00-\u0E7F]+)
DEMO
Test
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"([\u0E00-\u0E7F]+)"
test_str = "engภาษาไทยที่มีสระ123!#"
matches = re.finditer(regex, test_str, re.MULTILINE | re.UNICODE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Demo
const regex = /([\u0E00-\u0E7F]+)/gmu;
const str = `engภาษาไทยที่มีสระ123!#`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
RegEx
If this expression wasn't desired, it can be modified or changed in regex101.com.
RegEx Circuit
jex.im visualizes regular expressions:
Reference
Regular Expression to accept all Thai characters and English letters in python
Update 2: https://regex101.com/r/bE5aWW/2
Update: This is what I can come up with so far, https://regex101.com/r/bE5aWW/1/, but need help to get rid of .
Case 1
\n \n by name name\n \n
Case 2
\n \n name name\n \n
Case 3
by name name
Case 4
name name
I would like to select the name part from the above strings, i.e. name name. The one I came up with, (?:by)? ([\w ]+) donesn't work when there are spaces before by.
Thanks
Codes from regex101
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:by)? ([\w ]+)"
test_str = ("\\n \\n by Ally Foster\\n \\n \n\n"
"\\n \\n Ally Foster\\n \\n \n\n"
"by name name\n\n"
"name name")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
(?:by )?(\b(?!by\b)[\w, ]+\S)
My final version, which also won't select strings only have by
I suggest using
re.findall(r'\b(?!by\b)[^\W\d_]+(?: *(?:, *)?[^\W\d_]+)*', s)
See the regex demo. In Python 2, you will need to pass re.U flag to make all the shorthand character classes and the word boundary Unicode aware. To also match tabs rather than just spaces, replace spaces with [ \t].
Details
\b - a word boundary
(?!by\b) - the next word cannot be by
[^\W\d_]+ - one or more letters
(?: *(?:, *)?[^\W\d_]+)* - a non-capturing group that matches 0 or more occurrences of:
* - zero or more spaces
(?:, *)? - an optional sequence of , and 0+ spaces
[^\W\d_]+ - one or more letters.