LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2013, 02:43 AM   #1
exodius
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Rep: Reputation: 0
Python regex question


Hi everyone!
Newbie question with python regex:
How should i write to match string 'block' and 'blok' ?
Python version 2.7
Thanks in advance!
 
Old 12-11-2013, 05:58 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Code:
import re

s = 'block'
m = re.search('bl?ock', s):
if m:
    … do something here
Note the question mark - it means that previous character ('l' in this case) is optional.

Take a look at this tutorial for example.

Last edited by firstfire; 12-11-2013 at 06:03 AM. Reason: Add a tutorial link
 
1 members found this post helpful.
Old 12-11-2013, 06:28 AM   #3
exodius
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Thank you, that helpded!
 
Old 12-11-2013, 10:17 AM   #4
brianadams
LQ Newbie
 
Registered: Dec 2013
Posts: 3

Rep: Reputation: Disabled
Quote:
Originally Posted by exodius View Post
Hi everyone!
Newbie question with python regex:
How should i write to match string 'block' and 'blok' ?
Python version 2.7
Thanks in advance!
Code:
if mystring in ["block","blok"]
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Reading Individual files and Regex through them metallica1973 Programming 8 11-19-2013 04:39 PM
[SOLVED] Python Newbie Question Regex metallica1973 Programming 4 03-06-2013 05:34 PM
[SOLVED] Python Regex Question mwwynne Programming 6 12-10-2012 11:58 PM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Regex search in files with python smokybobo Programming 0 11-08-2003 06:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration