-6

Below is my string pattern,

'YES-HIDETotal Maze LLC.'

& I want to split the above string into below list,

 ['YES-HIDE', 'Total Maze LLC.']

How can I do this using regex in python?

Edit:

I'd like to split a string on a capital letter next to a capital letter followed by a lowercase letter using re package

To provide another example

'PLEASE SPLITThis String'

into

['PLEASE SPLIT', 'This String']
kygcoleman
  • 734
  • 15
  • 25
  • 2
    Only this specific case? Or you have a logic rule to implement? – Manuel Fedele Mar 18 '19 at 12:49
  • please also share the other sample input string. – Usman Mar 18 '19 at 12:50
  • 1
    It would probably be a good idea to put that information from the title into the question. Apparently I'm not the only one around here who's blind to titles. – Aran-Fey Mar 18 '19 at 12:51
  • 1
    Now that you have clarified your question, please show us some of your work on the problem. What have you tried, and just where are you stuck? How much about regular expressions do you understand? By "regex" do you mean [the third-party module by that name](https://pypi.org/project/regex/) or do you mean the `re` module or some other module? – Rory Daulton Mar 18 '19 at 12:55
  • @RoryDaulton can't use the re tag yet unfortunately but I have no understanding of regex at all but I know it's a way to manipulate strings with a specific format – kygcoleman Mar 18 '19 at 12:57
  • Can you show more examples? What about other characters than letters in your string? Is it always only two strings? Is a pure pythonic answer accepted? – Dschoni Mar 18 '19 at 12:59
  • 1
    If you do not understand regular expressions at all, this question is too broad for this site, since this is not a tutorial site. There are many tutorials available, online and as books. (A popular book that includes this topic is *Automate the Boring Stuff with Python*.) You should read up on regular expressions, try some simpler problems, then attempt this problem. If that would take too long, this problem can be solved fairly easily with pure Python--have you attempted that? – Rory Daulton Mar 18 '19 at 13:00
  • @kygcoleman you may consider accepting an answer that helped you: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work cheers – DirtyBit Mar 21 '19 at 06:06
  • I would love to add my answer, but I guess I can't control my own questions. I've come up with a solution using the boolean functions `islower()` and `isupper()` inside a for loop – kygcoleman Mar 21 '19 at 13:26

2 Answers2

3

I'm trying to give an answer that will help you understanding the problem and work on the solution.

You have a string with capital letters and at some point there is a small letter. You want the string to be split at the position before the first small letter. You can iterate through the string and find the first small letter, remember that position and split the string there.

This is neither regex nor fast, but simple and verbose:

input_string = 'TESTTest'
for pos, letter in enumerate(input_string):
  if letter.islower() and letter.isalpha():
    split_position = pos-1
    break
first_part = input_string[:split_position]
second_part = input_string[split_position:]
Dschoni
  • 3,714
  • 6
  • 45
  • 80
0

Try Regex: (?<=[A-Z])[A-Z](?=[a-z])

Regex Demo

Python Demo

Matt.G
  • 3,586
  • 2
  • 10
  • 23