简体   繁体   中英

Regex Exclude Part Of String

I need to extract a Part Of String through regex but i am finding little bit of problem please help

This is the output

Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
<None>

User profiles
-------------
All User Profile : Root Of Pi
All User Profile : Redmi
All User Profile : Root Of Pi 2

So I want to Extract only the name after: Example: All User Profile: Root Of Pi in this i want to extract only Root Of Pi

So i have tried my regex code here but it is grabbing all the string including All user profile

here is my code ((?:All User Profile\s*:)(.*))

I want to exclude this part All User Profile:

Thank You For the help

I am assuming that you want extract the names after All User Profile .

>>> import re
>>>
>>> p = re.compile(r"All User Profile : (.*)")
>>> string = """Profiles on interface Wi-Fi:
...
... Group policy profiles (read only)
... ---------------------------------
... <None>
...
... User profiles
... -------------
... All User Profile : Root Of Pi
... All User Profile : Redmi
... All User Profile : Root Of Pi 2
... """
>>>
>>> p.findall(string)
['Root Of Pi', 'Redmi', 'Root Of Pi 2']

Note, this solution assumes there is only a single colon, and we would like to extract everything after it:

import re
re.findall(r'.* \: (.*)', string1)

Data:

string1 = """Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
<None>

User profiles
-------------
All User Profile : Root Of Pi
All User Profile : Redmi
All User Profile : Root Of Pi 2
"""

Base on your issue.

import re

regex = r"^((\s*|.*\W)(((\w+)\s*)+)?)?[:]\s*(.*)$"

test_str = ("User profiles\n"
    "-------------\n\n"
    "All User Profile : Root Of Pi\n"
    "All User Profile : Redmi\n"
    "All User Profile : Root Of Pi 2\n"
    "User Profile: Redmi2\n"
    ": Redmi3\n"
    "#$%^&*wdw : Redmi4\n"
    "#$%^ : Redmi5\n\n\n")

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)))

Output:

Match 1 was found at 28-58:  
All User Profile : Root Of Pi
Group 1 found at 28-46:
All User Profile
Group 2 found at 28-29:

Group 3 found at 29-46: All User Profile 
Group 4 found at 38-46: Profile
Group 5 found at 38-45: Profile
Group 6 found at 48-58: Root Of Pi
Match 2 was found at 59-83: All User Profile : Redmi
Group 1 found at 59-76: All User Profile
Group 2 found at 59-59:
Group 3 found at 59-76: All User Profile
Group 4 found at 68-76: Profile
Group 5 found at 68-75: Profile
Group 6 found at 78-83: Redmi
Match 3 was found at 84-115: All User Profile : Root Of Pi 2
Group 1 found at 84-101: All User Profile
Group 2 found at 84-84:
Group 3 found at 84-101: All User Profile
Group 4 found at 93-101: Profile
Group 5 found at 93-100: Profile
Group 6 found at 103-115: Root Of Pi 2
Match 4 was found at 116-136: User Profile: Redmi2
Group 1 found at 116-128: User Profile
Group 2 found at 116-116:
Group 3 found at 116-128: User Profile
Group 4 found at 121-128: Profile
Group 5 found at 121-128: Profile
Group 6 found at 130-136: Redmi2
Match 5 was found at 137-145: : Redmi3
Group 1 found at 137-137:
Group 2 found at 137-137:
Group 3 found at -1--1: None
Group 4 found at -1--1: None
Group 5 found at -1--1: None
Group 6 found at 139-145: Redmi3
Match 6 was found at 146-164: #$%^&*wdw : Redmi4
Group 1 found at 146-156: #$%^&*wdw
Group 2 found at 146-156: #$%^&*wdw
Group 3 found at -1--1: None
Group 4 found at -1--1: None
Group 5 found at -1--1: None
Group 6 found at 158-164: Redmi4
Match 7 was found at 165-178: #$%^ : Redmi5
Group 1 found at 165-170: #$%^
Group 2 found at 165-170: #$%^
Group 3 found at -1--1: None
Group 4 found at -1--1: None
Group 5 found at -1--1: None
Group 6 found at 172-178: Redmi5

You can check at link:

https://regex101.com/r/1C5l2B/2

I think this is enough for you to update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM