473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

regex + string formatting?

Can I regex (dont know if that's truely a verb, but i digress...) in string functions such as split, replace or strip?

What i want to do is with a file that has lines like

asd5Jkl;lk
132bn8K;lk

I want to split them whenever i have a number followed by a capital letter.

I immediatly thought usinng regex and somehing like pattern = "[0-9][A-Z]"

Ideally i'd split it between the number and letter, say with a '\n' so
[0-9][A-Z] would become [0-9] \n [A-Z]

any thoughts on this?
Jan 25 '08 #1
2 1641
bvdet
2,851 Expert Mod 2GB
Can I regex (dont know if that's truely a verb, but i digress...) in string functions such as split, replace or strip?

What i want to do is with a file that has lines like

asd5Jkl;lk
132bn8K;lk

I want to split them whenever i have a number followed by a capital letter.

I immediatly thought usinng regex and somehing like pattern = "[0-9][A-Z]"

Ideally i'd split it between the number and letter, say with a '\n' so
[0-9][A-Z] would become [0-9] \n [A-Z]

any thoughts on this?
The following will break a string by slicing at the match and join the strings with '\n'.
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. def split_on_re(s):
  4.     # split a string between the number and capital letter
  5.     patt = re.compile(r'([0-9])(?=[A-Z])')
  6.     while True:
  7.         m = patt.search(s)
  8.         if m:
  9.             s = '\n'.join([s[:m.end()], s[m.end():]])
  10.         else:
  11.             break
  12.     return s
  13.  
  14. print repr(split_on_re('asd5Jkl;lk132bn8K;lk8J'))
  15.  
  16. >>> 'asd5\nJkl;lk132bn8\nK;lk8\nJ'
Jan 25 '08 #2
ghostdog74
511 Expert 256MB
Can I regex (dont know if that's truely a verb, but i digress...) in string functions such as split, replace or strip?

What i want to do is with a file that has lines like

asd5Jkl;lk
132bn8K;lk

I want to split them whenever i have a number followed by a capital letter.

I immediatly thought usinng regex and somehing like pattern = "[0-9][A-Z]"

Ideally i'd split it between the number and letter, say with a '\n' so
[0-9][A-Z] would become [0-9] \n [A-Z]

any thoughts on this?
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. for line in open("file"):
  3.     for n,l in enumerate(line):
  4.         if l.isdigit() and line[n+1].isupper():
  5.             sys.stdout.write(l + "\n")
  6.         else: sys.stdout.write(l)
  7.  
Jan 28 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: lawrence | last post by:
When users enter urls or other long strings it can destroy the formatting of a page. A long url, posted in a comment, can cause page distortions that make the page unreadable, till the website...
8
by: G. Stewart | last post by:
The objective is to extract the first n characters of text from an HTML block. I wish to preserve all HTML (links, formatting etc.), and at the same time, extend the size of the block to ensure...
6
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
4
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
2
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */...
2
by: mdaWeb335 | last post by:
Hi I'm trying to use RegEx to remove a tag by it's ID attribute. The actual HTML has already been cleaned up as I am formatting for export to Excel (so no need to point out that it's incorrect...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.