'Two groups of alphanumeric characters (including letters, numbers, and underscores) separated by one or more whitespace characters
Fill in the code to check if the text passed has at least 2 groups of alphanumeric characters (including letters, numbers, and underscores) separated by one or more whitespace characters
import re
def check_character_groups(text):
result = re.search(r"___", text)
return result != None
print(check_character_groups("One")) # False
print(check_character_groups("123 Ready Set GO")) # True
print(check_character_groups("username user_01")) # True
print(check_character_groups("shopping_list: milk, bread, eggs.")) # False
Solution 1:[1]
import re
def check_character_groups(text):
result = re.search(r"\w\s+\w", text)
return result != None
print(check_character_groups("One ")) # False
print(check_character_groups("123 Ready Set GO")) # True
print(check_character_groups("username user_01")) # True
print(check_character_groups("shopping_list: milk, bread, eggs.")) # False
This is my answer that returns what it is asking for,
the key answer is \w\s+\w
Solution 2:[2]
Use as regex pattern
\b\w+\s+\w+\b
\b
word boundery, marks a start of a word (also works as start of string)\w+
means alphanumeric characters[a-zA-Z0-9_]
(one or more)\s+
one or more whitespaces\w+
next alphanumeric characters (1+)\b
word boundery (works at string end too)
So try:
import re
def check_character_groups(text):
result = re.search(r"\b\w+\s+\w+\b", text)
return result != None
print(check_character_groups("One ")) # False
print(check_character_groups("123 Ready Set GO")) # True
print(check_character_groups("username user_01")) # True
print(check_character_groups("shopping_list: milk, bread, eggs.")) # False
If there should be special characters/spaces/... left and/or right of the alphanumeric groups, you must specify it.
Test it on regex101 or debuggex.
Picture by Regulex (! The syntax there is for JavaScript !)
Solution 3:[3]
import re
def check_character_groups(text):
result = re.search(r"\w+\s", text)
return result != None
print(check_character_groups("One")) # False
print(check_character_groups("123 Ready Set GO")) # True
print(check_character_groups("username user_01")) # True
print(check_character_groups("shopping_list: milk, bread, eggs.")) # False
Here is your output: False True True False
Solution 4:[4]
import re
def check_character_groups(text):
result = re.search(r"[\w]+[\s]+[\w]", text)
return result != None
The answer is to use [\w]+[\s]+[\w]
as regex.
Solution 5:[5]
This will match the condition for 2 groups of alphanumeric characters including underscore separated by one or more whitespace characters
result = re.search(r"\w+\s.*", text)
Clarification:
- Repeated characters will be searched by *
- Alphanumeric characters including underscore will be searched by \w
- Space characters will be searched by using \s
- To search character between two will be searched by a combination of a dot(.)
Solution 6:[6]
result = re.search(r"\b\w*\s\w*\b", text)
You can try this too. \b defines the boundary.
Solution 7:[7]
Use simple one
result = re.search(r"[\w] [\w]", text)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Divya Goswami |
Solution 4 | Ank |
Solution 5 | |
Solution 6 | Shravan Palan |
Solution 7 | manthan gehlot |