How to separate part of the string where it is all upper case?

How do separate parts of the string where it is all upper case?


Cover Image of How to separate part of the string where it is all upper case?
Cover Image of How to separate part of the string where it is all upper case?


 To separate the part of a string where it is all upper case, you can use regular expressions in most programming languages.


Here is an example of Python code to extract the uppercase substrings:


Python

Copy code

import re


string = "HELLO WORLD I AM HERE"

uppercase_substrings = re.findall(r'\b[A-Z]+\b', string)


print(uppercase_substrings)



code image of  separate part of the string where it is all upper case?
code image of  separate part of the string where it is all upper case?




The regular expression \b[A-Z]+\b matches any substring that contains only uppercase letters and is delimited by word boundaries (i.e., spaces, punctuation, or the beginning/end of the string). The re.findall() function returns a list of all non-overlapping matches.


The output of the above code will be:


css

Copy code

['HELLO', 'WORLD', 'I', 'AM', 'HERE']


You can modify the regular expression to fit the specific needs of your string.

Post a Comment

Previous Post Next Post