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

Finding # prefixing numbers

In a text that contains references to numbers like this: #583 I want to
find them with a regular expression but I'm having problems with the
hash. Hopefully this code explains where I'm stuck:
import re
re.compile(r'\b(\d\d\d)\b').findall('#123 x (#234) or:#456 #6789') ['123', '234', '456'] re.compile(r'\b(X\d\d\d)\b').findall('X123 x (X234) or:X456 X6789') ['X123', 'X234', 'X456'] re.compile(r'\b(#\d\d\d)\b').findall('#123 x (#234) or:#456 #6789') [] re.compile(r'\b(\#\d\d\d)\b').findall('#123 x (#234) or:#456 #6789')

[]

As you can guess, I'm trying to find a hash followed by 3 digits word
bounded. As in the example above, it wouldn't have been a problem if
the prefix was an 'X' but that's not the case here.

Jul 21 '05 #1
3 1362
pe*****@gmail.com wrote:
In a text that contains references to numbers like this: #583 I want
to find them with a regular expression but I'm having problems with
the hash. Hopefully this code explains where I'm stuck:
import re
re.compile(r'\b(\d\d\d)\b').findall('#123 x (#234) or:#456 #6789') ['123', '234', '456'] re.compile(r'\b(X\d\d\d)\b').findall('X123 x (X234) or:X456 X6789') ['X123', 'X234', 'X456'] re.compile(r'\b(#\d\d\d)\b').findall('#123 x (#234) or:#456 #6789') [] re.compile(r'\b(\#\d\d\d)\b').findall('#123 x (#234) or:#456
#6789') []

As you can guess, I'm trying to find a hash followed by 3 digits word
bounded. As in the example above, it wouldn't have been a problem if
the prefix was an 'X' but that's not the case here.


From the re documentation:
\b
Matches the empty string, but only at the beginning or end of a word.
A word is defined as a sequence of alphanumeric or underscore
characters, so the end of a word is indicated by whitespace or a
non-alphanumeric, non-underscore character. Note that \b is defined as
the boundary between \w and \ W, so the precise set of characters
deemed to be alphanumeric depends on the values of the UNICODE and
LOCALE flags. Inside a character range, \b represents the backspace
character, for compatibility with Python's string literals.


# is not a letter or digit, so \b# will match only if the # is directly
preceded by a letter or digit which isn't the case in any of your examples.
Use \B (which is the opposite of \b) instead:
re.compile(r'\B(#\d\d\d)\b').findall('#123 x (#234) or:#456 #6789')

['#123', '#234', '#456']

Jul 21 '05 #2
Thank you! That solved my problem.

Jul 21 '05 #3
You really owe it to yourself to try the PyParsing package, if you have to
do this kind of thing with any frequency.

The syntactic difference between PyParsing and regular expressions is
greater than the syntactic difference between Python and C.

thx
Caleb

On Tue, 19 Jul 2005 13:35:02 +0200, pe*****@gmail.com <pe*****@gmail.com>
wrote:
In a text that contains references to numbers like this: #583 I want to
find them with a regular expression but I'm having problems with the
hash. Hopefully this code explains where I'm stuck:
import re
re.compile(r'\b(\d\d\d)\b').findall('#123 x (#234) or:#456 #6789') ['123', '234', '456'] re.compile(r'\b(X\d\d\d)\b').findall('X123 x (X234) or:X456 X6789') ['X123', 'X234', 'X456'] re.compile(r'\b(#\d\d\d)\b').findall('#123 x (#234) or:#456 #6789') [] re.compile(r'\b(\#\d\d\d)\b').findall('#123 x (#234) or:#456 #6789')

[]

As you can guess, I'm trying to find a hash followed by 3 digits word
bounded. As in the example above, it wouldn't have been a problem if
the prefix was an 'X' but that's not the case here.


Jul 21 '05 #4

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

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
10
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
6
by: GSpiggle | last post by:
I have a database with records that should have a consecutive number ID (Check Register). Also has other records (deposits and adjusting entries) that don't fit in the number range so...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
4
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
19
by: ramu | last post by:
Hi, I have, suppose 1000 numbers, in a file. I have to find out 5 largest numbers among them without sorting. Can you please give me an efficient idea to do this? My idea is to put those numbers...
12
by: e271828 | last post by:
Hi, I'm helping to work a developer tool that verifies a given HTML element has a given attribute (e.g., that all LABEL elements have a FOR attribute, all INPUT elements have an ID attribute,...
10
by: strife | last post by:
Hi, This is a homework question. I will try to keep it minimal so not to have anyone do it for me. I am really just stuck on one small spot. I have to figure out the highest number from a users...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.