473,654 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert characters into string based on re ?

I am attempting to reformat a string, inserting newlines before certain
phrases. For example, in formatting SQL, I want to start a new line at
each JOIN condition. Noting that strings are immutable, I thought it
best to spllit the string at the key points, then join with '\n'.

Regexps can seem the best way to identify the points in the string
('LEFT.*JOIN' to cover 'LEFT OUTER JOIN' and 'LEFT JOIN'), since I need
to identify multiple locationg in the string. However, the re.split
method returns the list without the split phrases, and re.findall does
not seem useful for this operation.

Suggestions?

Oct 12 '06 #1
7 2362

Matt wrote:
I am attempting to reformat a string, inserting newlines before certain
phrases. For example, in formatting SQL, I want to start a new line at
each JOIN condition. Noting that strings are immutable, I thought it
best to spllit the string at the key points, then join with '\n'.

Regexps can seem the best way to identify the points in the string
('LEFT.*JOIN' to cover 'LEFT OUTER JOIN' and 'LEFT JOIN'), since I need
to identify multiple locationg in the string. However, the re.split
method returns the list without the split phrases
Not without some minor effort on your part :-)
See below.
and re.findall does
not seem useful for this operation.

Suggestions?
Read the fine manual:
"""
split( pattern, string[, maxsplit = 0])

Split string by the occurrences of pattern. If capturing parentheses
are used in pattern, then the text of all groups in the pattern are
also returned as part of the resulting list. If maxsplit is nonzero, at
most maxsplit splits occur, and the remainder of the string is returned
as the final element of the list. (Incompatibilit y note: in the
original Python 1.5 release, maxsplit was ignored. This has been fixed
in later releases.)
>>re.split('\W+ ', 'Words, words, words.')
['Words', 'words', 'words', '']

# Now see what happens when you use capturing parentheses:
>>re.split('(\W +)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>re.split('\W+ ', 'Words, words, words.', 1)
['Words', 'words, words.']
"""

HTH,
John

Oct 12 '06 #2

Matt wrote:
I am attempting to reformat a string, inserting newlines before certain
phrases. For example, in formatting SQL, I want to start a new line at
each JOIN condition. Noting that strings are immutable, I thought it
best to spllit the string at the key points, then join with '\n'.

Regexps can seem the best way to identify the points in the string
('LEFT.*JOIN' to cover 'LEFT OUTER JOIN' and 'LEFT JOIN'), since I need
to identify multiple locationg in the string. However, the re.split
method returns the list without the split phrases, and re.findall does
not seem useful for this operation.

Suggestions?
I think that re.sub is a more appropriate method rather than split and
join

trivial example (non SQL):
>>addnlre = re.compile('LEF T\s.*?\s*JOIN|R IGHT\s.*?\s*JOI N', re.DOTALL + re.IGNORECASE). sub
addnlre(lambd a x: x.group() + '\n', '... LEFT JOIN x RIGHT OUTER join y')
'... LEFT JOIN\n x RIGHT OUTER join\n y'

Oct 13 '06 #3
ha***********@i nforma.com wrote:
>
Matt wrote:
>I am attempting to reformat a string, inserting newlines before
certain phrases. For example, in formatting SQL, I want to start a
new line at each JOIN condition. Noting that strings are immutable, I
thought it best to spllit the string at the key points, then join
with '\n'.

I think that re.sub is a more appropriate method rather than split and
join

trivial example (non SQL):
>>>addnlre = re.compile('LEF T\s.*?\s*JOIN|R IGHT\s.*?\s*JOI N',
re.DOTALL + re.IGNORECASE). sub addnlre(lambda x: x.group() + '\n',
'... LEFT JOIN x RIGHT OUTER join y')
'... LEFT JOIN\n x RIGHT OUTER join\n y'

Quite apart from the original requirement being to insert newlines before
rather than after the phrase, I wouldn't have said re.sub was appropriate.
>>addnlre(lambd a x: x.group() + '\n',
"select * from whatever where action in ['user left site', 'user joined site']")
"select * from whatever where action in ['user left site', 'user join\ned site']"

or with the newline before the pattern:
>>addnlre(lambd a x: '\n'+x.group(),
"select * from whatever where action in ['user left site', 'user joined site']")
"select * from whatever where action in ['user \nleft site', 'user joined site']"

Oct 13 '06 #4
Matt wrote:
I am attempting to reformat a string, inserting newlines before certain
phrases. For example, in formatting SQL, I want to start a new line at
each JOIN condition. Noting that strings are immutable, I thought it
best to spllit the string at the key points, then join with '\n'.

Regexps can seem the best way to identify the points in the string
('LEFT.*JOIN' to cover 'LEFT OUTER JOIN' and 'LEFT JOIN'), since I need
to identify multiple locationg in the string. However, the re.split
method returns the list without the split phrases, and re.findall does
not seem useful for this operation.

Suggestions?

Matt,

You may want to try this solution:
>>import SE
>>Formatter = SE.SE (' "~(?i)(left|inn er|right|outer) .*join~=\n=" ')
# Details explained below the dotted line
>>print Formatter ('select id, people.* from ids left outer join
people where ...\nSELECT name, job from people INNER JOIN jobs WHERE
....;\n')
select id, people.* from ids
left outer join people where ...
SELECT name, job from people
INNER JOIN jobs where ...;

You may add other substitutions as required one by one, interactively
tweaking each one until it does what it is supposed to do:
>>Formatter = SE.SE ('''
"~(?i)(left|inn er|right|outer) .*join~=\n =" # Add an indentation
"where=\n where" "WHERE=\n WHERE" # Add a newline also
before 'where'
";\n=;\n\n" # Add an extra line feed
"\n=;\n\n" # And add any missing
semicolon
# etc.
''')
>>print Formatter ('select id, people.* from ids left outer join
people where ...\nSELECT name, job from people INNER JOIN jobs WHERE
....;\n')
select id, people.* from ids
left outer join people
where ...;

SELECT name, job from people
INNER JOIN jobs
WHERE ...;
http://cheeseshop.python.org/pypi?:a...SE&version=2.3
Frederic
----------------------------------------------------------------------------------------------------------------------

The anatomy of a replacement definition
>>Formatter = SE.SE (' "~(?i)(left|inn er|right|outer) .*join~=\n=" ')
target=substitu te (first '=')
>>Formatter = SE.SE (' "~(?i)(left|inn er|right|outer) .*join~=\n=" ')
= (each
following '=' stands for matched target)
>>Formatter = SE.SE (' "~(?i)(left|inn er|right|outer) .*join~=\n=" ')
~ ~ (contain
regular expression)
>>Formatter = SE.SE (' "~(?i)(left|inn er|right|outer) .*join~=\n=" ')
" "
(contain definition containing white space)

Oct 14 '06 #5
Frederic Rentsch wrote:
Matt wrote:
>I am attempting to reformat a string, inserting newlines before certain
phrases. For example, in formatting SQL, I want to start a new line at
each JOIN condition. Noting that strings are immutable, I thought it
best to spllit the string at the key points, then join with '\n'.

Regexps can seem the best way to identify the points in the string
('LEFT.*JOIN ' to cover 'LEFT OUTER JOIN' and 'LEFT JOIN'), since I need
to identify multiple locationg in the string. However, the re.split
method returns the list without the split phrases, and re.findall does
not seem useful for this operation.

Suggestions?


Matt,

You may want to try this solution:
>import SE
.... snip
>
http://cheeseshop.python.org/pypi?:a...SE&version=2.3
For reasons unknown, the new download for SE is on the old page:
http://cheeseshop.python.org/pypi/SE/2.2%20beta.
>

Frederic
----------------------------------------------------------------------------------------------------------------------
Oct 15 '06 #6
Hi,
initially I had the same idea before I started writing a SQL Formatter.
I was sure that coding a few "change" commands in a script would
reformat my SQL statements. But step by step I recognized that SQL
statements can not be formatted by regular expressions. Why not?
Because there is a risk that you change e.g. values in literals and
this is changing the result of a query!!
Example:

--Select pieces where status like "Join with master piece"

Inserting line-breaks before joins using a "change" command would
change the SQL statement into

--Select pieces where status like "\nJoin with master piece"

The new select statement is no more working in the same way as the
original one.

In the meantime, the "script" has about 80 pages of code .....

Regards
GuidoMarcel

Oct 19 '06 #7

You can test it here: http://www.sqlinform.com

Oct 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
22612
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double quotes? Do I need to use code to scrub each string and remove or escape the double quotes before using it in a query? The error I get is this: Error Number 3075: Syntax error (missing operator) in query expression '"credit card billed by...
14
7582
by: Adam Clauss | last post by:
I've an application which is using a multiline textbox to log the status of a fairly long procedure. "Updates" are made to the status by calling textbox.AppendText. As my task is fairly lengthy, I go well past the default max text length of the textbox. According to the documentation, setting MaxLength = 0 will increase this to basically the limit of available memory. So, in the form designer, I set the MaxLength property equal to 0. ...
13
5581
by: Matt | last post by:
10 len space designated strings grows when i do sb.insert. is there way to stop string growing. It becomes 14 space len on test.file I like to be able insert 3rd position but length should stay 10 string initialValue = " "; string xyz = "xyz"; sb = new StringBuilder(initialValue); sb.Insert(3, xyz);
11
3784
by: anony | last post by:
Hello, I can't figure out why my parameterized query from an ASP.NET page is dropping "special" characters such as accented quotes & apostrophes, the registered trademark symbol, etc. These symbols insert without problem from query analyzer, so that suggests it's something within ASP.NET. I've tried using .NET textbox web controls as well as html textareas. I have a test database set up with 4 fields: varchar, nvarchar, text, and...
9
3670
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in appropriately moved further down? From a cursory search of the libc documentation I can't find such a function. Will I have to write it myself? Thanks.
6
3449
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am using MS-Access 2000 database table for this app. Note that the datatype of all the fields mentioned above are Text. Apart from the above columns, there's another column in the DB table named 'RegDateTime' whose datatype is Date/Time which is...
12
1848
by: Torsten Bronger | last post by:
Hallöchen! I need some help with finding matches in a string that has some characters which are marked as escaped (in a separate list of indices). Escaped means that they must not be part of any match. My current approach is to look for matches in substrings with the escaped characters as boundaries between the substrings. However, then ^ and $ in the patterns are treated wrongly. (Although I use startpos and endpos parameters for...
6
3866
by: TheRealDan | last post by:
Hi all. I'm having a problem with a special characters. I have php script that reads from an xml file and writes to a mysql db. It's a script called phptunest that I found on the net, although the original website for the author appears to be gone. It works really nicely except when it hits special characters. Everything from a sp char forward just gets lost. It is using mysql_real_escape_string, but that doesn't seem to help with the ...
2
2994
by: parasuram | last post by:
Hi friends ............. this is a question regarding the data structures trees Pleas post it if possible with in 2 days I will thankful if some body could help doing this. Operating system: windows xp compiler : Dev c++ Question is as folllows : Genaration of keys : Assume that your keys are character strings of length 10 obtained by scannig a c++ program. If a string has less than 10 characters make it up to 10 characters by...
2
30149
by: franc sutherland | last post by:
Hello, I am using Access 2003. Is it possible to use string variables in the INSERT INTO statement? I am using the INSERT INTO statement to add a long list of contacts to a group by looping through the recordset (based on a linked spreadsheet, 'tbl_group_import') and using the INSERT INTO statement on each loop. I am using the rst.Fields.Item("FieldName") method to
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.