473,395 Members | 1,468 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,395 software developers and data experts.

One more regular expressions question

I have a couple of strings like:

Unassigned Number (1) 32
No Route To Destination (3) 12
Normal call clearing (16) 2654
User busy (17) 630
No user respond (18) 5
User alerting no answer (19) 16
Call rejected (21) 3
Destination out of order (27) 1
Invalid number format (28) 32
Normal unspecified (31) 32
No channel available (34) 2
Temporary failure (41) 11
Switching equipment congestion (42) 4
Resource unavailable unspecified (47) 2
Bearer capability not authorized (57) 73
Incomp. dest. / Non-existent CUG (88) 1
Recovery on timer expiry (102) 2
Interworking, unspecified (127) 5

I need to get:
Error code (value in brackets) - Value - Message.

My actual problem is i can't get how to include space, comma, slash.

Jan 18 '07 #1
9 1168
Victor Polukcht wrote:
My actual problem is i can't get how to include space, comma, slash.
Post here what you have written already, so we can tell you what the
problem is.

--
Roberto Bonvallet
Jan 18 '07 #2
Victor Polukcht wrote:
I have a couple of strings like:

Unassigned Number (1) 32
No Route To Destination (3) 12
Normal call clearing (16) 2654
User busy (17) 630
No user respond (18) 5
User alerting no answer (19) 16
Call rejected (21) 3
Destination out of order (27) 1
Invalid number format (28) 32
Normal unspecified (31) 32
No channel available (34) 2
Temporary failure (41) 11
Switching equipment congestion (42) 4
Resource unavailable unspecified (47) 2
Bearer capability not authorized (57) 73
Incomp. dest. / Non-existent CUG (88) 1
Recovery on timer expiry (102) 2
Interworking, unspecified (127) 5

I need to get:
Error code (value in brackets) - Value - Message.

My actual problem is i can't get how to include space, comma, slash.
The following solution is 100% regex-free :-)
>>print lines
Unassigned Number (1) 32
No Route To Destination (3) 12
Normal call clearing (16) 2654
User busy (17) 630
No user respond (18) 5
User alerting no answer (19) 16
Call rejected (21) 3
Destination out of order (27) 1
Invalid number format (28) 32
Normal unspecified (31) 32
No channel available (34) 2
Temporary failure (41) 11
Switching equipment congestion (42) 4
Resource unavailable unspecified (47) 2
Bearer capability not authorized (57) 73
Incomp. dest. / Non-existent CUG (88) 1
Recovery on timer expiry (102) 2
Interworking, unspecified (127) 5
>>[int(line.rsplit("(", 1)[1].split(")", 1)[0]) for line in
lines.splitlines()]
[1, 3, 16, 17, 18, 19, 21, 27, 28, 31, 34, 41, 42, 47, 57, 88, 102, 127]

Peter
Jan 18 '07 #3
My pattern now is:

(?P<var1>[^(]+)(?P<var2>\d+)\)\s+(?P<var3>\d+)

And i expect to get:

var1 = "Unassigned Number "
var2 = "1"
var3 = "32"

I'm sure my regexp is incorrect, but can't understand where exactly.

Regex.debug shows that even the first block is incorrect.

Thanks in advance.

On Jan 18, 1:15 pm, Roberto Bonvallet <Roberto.Bonval...@cern.ch>
wrote:
Victor Polukcht wrote:
My actual problem is i can't get how to include space, comma, slash.Post here what you have written already, so we can tell you what the
problem is.

--
Roberto Bonvallet
Jan 18 '07 #4
Victor Polukcht wrote:
I have a couple of strings like:

Unassigned Number (1) 32
[...]
Interworking, unspecified (127) 5

I need to get:
Error code (value in brackets) - Value - Message.

My actual problem is i can't get how to include space, comma, slash.
Probably you have some escaping problem. The substitution:

re.sub(r"^(.*)\s*\((\d+)\)\s+(\d+)", r'\2 - \3 - \1', row)

does the required job (where "row" is one of your lines)

To match a special character, such as "(", you need to escape it with a
"\", because it has a special meaning in the regexp syntax. Because "\"
is the escaping mechanism for Python strings too, you better use raw
strings to specify the pattern.

Other special character/groups matching patterns, such as "\s" to
specify whitespaces, are documented, together with everything else you
need, at http://docs.python.org/lib/re-syntax.html

HTH

Daniele

Jan 18 '07 #5
Victor Polukcht wrote:
My pattern now is:

(?P<var1>[^(]+)(?P<var2>\d+)\)\s+(?P<var3>\d+)

And i expect to get:

var1 = "Unassigned Number "
var2 = "1"
var3 = "32"

I'm sure my regexp is incorrect, but can't understand where exactly.

Regex.debug shows that even the first block is incorrect.

Thanks in advance.

On Jan 18, 1:15 pm, Roberto Bonvallet <Roberto.Bonval...@cern.ch>
wrote:
Victor Polukcht wrote:
My actual problem is i can't get how to include space, comma, slash.Post here what you have written already, so we can tell you what the
problem is.

--
Roberto Bonvallet
You are missing \( after the first group. The RE should be:

'(?P<var1>[^(]+)\((?P<var2>\d+)\)\s+(?P<var3>\d+)'

Jan 18 '07 #6
Great thanks.

You post helped me so much!

My resulting regexp is:
"(?P<var1>^(.*)\s*)\(((?P<var2>\d+))\)\s+((?P<var3 >\d+))"

On Jan 18, 2:38 pm, "Daniele Varrazzo" <daniele.varra...@gmail.com>
wrote:
Victor Polukcht wrote:
I have a couple of strings like:
Unassigned Number (1) 32
[...]
Interworking, unspecified (127) 5
I need to get:
Error code (value in brackets) - Value - Message.
My actual problem is i can't get how to include space, comma, slash.Probably you have some escaping problem. The substitution:

re.sub(r"^(.*)\s*\((\d+)\)\s+(\d+)", r'\2 - \3 - \1', row)

does the required job (where "row" is one of your lines)

To match a special character, such as "(", you need to escape it with a
"\", because it has a special meaning in the regexp syntax. Because "\"
is the escaping mechanism for Python strings too, you better use raw
strings to specify the pattern.

Other special character/groups matching patterns, such as "\s" to
specify whitespaces, are documented, together with everything else you
need, athttp://docs.python.org/lib/re-syntax.html

HTH

Daniele
Jan 18 '07 #7
Victor Polukcht kirjoitti:
Great thanks.

You post helped me so much!

My resulting regexp is:
"(?P<var1>^(.*)\s*)\(((?P<var2>\d+))\)\s+((?P<var3 >\d+))"
If it doesn't have to be a regex:

#================================================= ==
s = '''\
Unassigned Number (1) 32
No Route To Destination (3) 12
Normal call clearing (16) 2654
User busy (17) 630
No user respond (18) 5
User alerting no answer (19) 16
Call rejected (21) 3
Destination out of order (27) 1
Invalid number format (28) 32
Normal unspecified (31) 32
No channel available (34) 2
Temporary failure (41) 11
Switching equipment congestion (42) 4
Resource unavailable unspecified (47) 2
Bearer capability not authorized (57) 73
Incomp. dest. / Non-existent CUG (88) 1
Recovery on timer expiry (102) 2
Interworking, unspecified (127) 5
'''

for row in s.split('\n')[:-1]:
var1, var2 = row.split('(')
var2, var3 = var2.split()
var2 = var2[:-1]
print var2, var3, var1
#================================================= ==

Cheers,
Jussi
Jan 18 '07 #8
Victor Polukcht wrote:
Great thanks.

You post helped me so much!

My resulting regexp is:
"(?P<var1>^(.*)\s*)\(((?P<var2>\d+))\)\s+((?P<var3 >\d+))"
Notice that this way you are including trailing whitespaces in the var1
group. You may want to put the "\s*" outside the parenthesis.

mmm... in this case you should make the ".*" in the first group
non-greedy. r"^(?P<var1>.*?)\s*\(((?P<var2>\d+))\)\s+((?P<var3 >\d+))"
does the job.

Bye

Daniele

Jan 18 '07 #9
On 2007-01-18, Victor Polukcht <vp*******@gmail.comwrote:
My pattern now is:

(?P<var1>[^(]+)(?P<var2>\d+)\)\s+(?P<var3>\d+)

And i expect to get:

var1 = "Unassigned Number "
var2 = "1"
var3 = "32"

I'm sure my regexp is incorrect, but can't understand where
exactly.
Break it up using verbose notation to help yourself. Also, use
more helpful names. With names like var1 and var2 you might as
well not used named groups.

r = re.compile(r"""(?x)
(?P<error[^(]+ )
(?P<errno\d+ )
\)
\s+
(?P<lineno\d+ )""")

This way it's clearer that there's a \) with no matching \(.

--
Neil Cerutti
This team is one execution away from being a very good basketball team. --Doc
Rivers

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '07 #10

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

Similar topics

20
by: Toby | last post by:
Could some tell how I could create a search replace Regular Express in .net where is would match MY_STRING_TO_BE_CONVERTED and replace with MyStringToBeConverted
7
by: Patient Guy | last post by:
Coding patterns for regular expressions is completely unintuitive, as far as I can see. I have been trying to write script that produces an array of attribute components within an HTML element. ...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
12
by: =?Utf-8?B?SlA=?= | last post by:
I am a newbie to regular expressions and want to extract a number from the end of a string. The string would have these formats: image/4567 image/45678 image/456789 I would also want to...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
9
by: Rene | last post by:
I'm trying to basically remove chunks of html from a page but I must not be doing my regular expression correctly. What i'm trying with no avail. $site = preg_replace("/<!DOCTYPE(.|\s)*<div...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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,...
0
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...
0
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...
0
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...

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.