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

quick regex question

Xx r3negade
Hi, I am very bad with regexes.
I need a regular expression that will reduce a url like this:

hxxp://example.com/somefolder/something/whatever

to its base:

hxxp://example.com

The two slashes after the http: complicate things. I know I could just remove the "http://" and add it again later, but as a learning experience, I would like to do this regex in a single line.

Thanks in advance
Apr 1 '09 #1
2 1454
erm..

import re
p = re.compile(r'http://.*?\.(com|co\.uk)')

you can obviously adjust the end to add as many different domains as you can think of.
Apr 1 '09 #2
bvdet
2,851 Expert Mod 2GB
The re solution may depend on the different possibilities of the URL name. Here is one possible solution:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. patt = re.compile(r'([a-z]+?:/+?\w+?\.\w+?)/')
  4.  
  5. m = patt.match("hxxp://example.com/somefolder/something/whatever")
  6. if m:
  7.      print m.group(1)
Apr 1 '09 #3

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

Similar topics

6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
4
by: Alex Moskalyuk | last post by:
We have a filename, which could theoretically be something like verylongfilename.2004.05.11.txt. We just need the significant part before the first dot. My solution was a quick regexp: filename...
1
by: K. Shier | last post by:
while writing a RegEx, you implicily name your Groups: \b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b so, why isn't .Name a property of a System.Text.RegularExpressions.RegEx.Group? ...
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 ...
5
by: Troy | last post by:
Hello, I'd like to validate an input my from user i.e. make sure that the user enters the phone number is the following formats (XXX) XXX-XXXX XXX-XXX-XXXX Any ideas on how I can achieve...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
10
by: yoni | last post by:
Hey, i am trying to get to the right regexp to remove everything that's a multi line comment. in other words, everything between \*...*/. my expression is: /\*.*\*/ Doesnt work... Anybody...
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?
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...
0
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...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.