Python Forum
Regex Include and Exclude patterns in Same Expression
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex Include and Exclude patterns in Same Expression
#1
How to split sep = "," at only ")," and exclude "%," ?

text = "METAL (3.25%,a/d=13/1),IT(2.31%,a/d=10/0),HEALTHCAREINDEX(1.32%,a/d=13/7)"
try1[No Work] > textSplit = text.split(r',') includes "%,"
try2[Works with issue]> textSplit = text.split(r'),') omits ")" at the end .How to add ")" back to the string.
try3[No Work]> textSplit = text.split(r'/(?<=[)],)/gm') positive lookahead doesn't split only at "),"

import re
import numpy as np

def randomColor(x):
    rColor = list(np.random.random(size=3) * 256) 
    return f'<span style = "color:rColor">{x}</span>'

def spanColor(text):
    textSplit =  text.split(r'/(?<=[)],)/gm')
    print(text)
    print(textSplit)
    textSplit_lst = [randomColor(x) for x in textSplit]
    textSplit_str = ",".join(textSplit_lst)

    return textSplit_str


def regex_func():
    print("Start")
    regStr = "METAL (3.25%,a/d=13/1),IT(2.31%,a/d=10/0),HEALTHCAREINDEX(1.32%,a/d=13/7)"
    regLst =  spanColor(regStr)
    print(f"regLst = {regLst}")


regex_func()
Reply
#2
What is the **expected** result
>>> text_split = text.split('),')
>>> text_split
['METAL (3.25%,a/d=13/1', 'IT(2.31%,a/d=10/0', 'HEALTHCAREINDEX(1.32%,a/d=13/7)']
>>> text_split[:-1] = (f'{item})' for item in text_split[:-1])
>>> text_split
['METAL (3.25%,a/d=13/1)', 'IT(2.31%,a/d=10/0)', 'HEALTHCAREINDEX(1.32%,a/d=13/7)']
your try2 looks like working, just add the ) back
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(May-23-2023, 02:38 AM)starzar Wrote: textSplit =  text.split(r'/(?<=[)],)/gm')
Where do you get this syntax from? Python is not Perl. The argument to str.split() is not a regex.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to include one script into another? MorningWave 8 501 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  [solved] Regex expression do not want to taken :/ SpongeB0B 2 774 Nov-06-2023, 02:43 PM
Last Post: SpongeB0B
  how include a python code in notpad++ plugin akbarza 2 654 Sep-25-2023, 08:25 PM
Last Post: deanhystad
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,475 May-14-2023, 04:29 PM
Last Post: Winfried
  The included URLconf 'scribimus.urls' does not appear to have any patterns in it. nengkya 0 1,075 Mar-03-2023, 08:29 PM
Last Post: nengkya
  How to remove patterns of characters from text aaander 4 1,124 Nov-19-2022, 03:34 PM
Last Post: snippsat
  Regex Expression With Code Query In Pandas eddywinch82 8 2,362 Apr-13-2022, 09:12 AM
Last Post: snippsat
  How to include input as part of variable name Mark17 4 2,526 Oct-01-2021, 06:45 PM
Last Post: Mark17
  Problem in Regex Expression shantanu97 2 1,718 Sep-28-2021, 03:40 AM
Last Post: shantanu97
  Using Regex Expression With Isin in Python eddywinch82 0 2,299 Apr-04-2021, 06:25 PM
Last Post: eddywinch82

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020