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

RegExp for a substring

Suppose I want to check that a string, $str, starts with at least the
first 3 letters of a given word, say "delete". Can I do that compactly
with a regular expression? The following are not my idea of compact:

preg_match("/^del(|e|et|ete)\\b/i", $str)
Quadratic in the length of the given word
preg_match("/^del(e(t(e)?)?)?\\b/i", $str)
Really now. But at least it's linear
Thanks.
Csaba Gabor from Vienna

Jun 14 '06 #1
3 2312
Rik
Csaba Gabor wrote:
Suppose I want to check that a string, $str, starts with at least the
first 3 letters of a given word, say "delete". Can I do that
compactly with a regular expression? The following are not my idea
of compact:

preg_match("/^del(|e|et|ete)\\b/i", $str)
Quadratic in the length of the given word
preg_match("/^del(e(t(e)?)?)?\\b/i", $str)
Really now. But at least it's linear


Euhm, why at LEAST 3?
You don't seem to be using 'more' matches.
Or is this just a quick example?

Normally, I wouldn't use regexes for this. The way you made them is about as
compact as they get for this particular purpose.

if(substr($str,0,3) = substr($needle,0,3){
//code...
}

If you're trying to do what I think you want, maybe this is the code for
you:
$needle = 'delete';
preg_match("/^[a-z]+\b/i", $str,$match);
if(strlen($match[0])>=3 && $match[0]==substr($needle,0,strlen($match[0]))){
//code if it matches
}

By no means shorter, but a lot more versatile.

Grtz,
--
Rik Wasmus
Jun 14 '06 #2
Rik wrote:
Csaba Gabor wrote:
Suppose I want to check that a string, $str, starts with at least the
first 3 letters of a given word, say "delete". Can I do that
compactly with a regular expression? The following are not my idea
of compact:

preg_match("/^del(|e|et|ete)\\b/i", $str)
Quadratic in the length of the given word

preg_match("/^del(e(t(e)?)?)?\\b/i", $str)
Really now. But at least it's linear
Euhm, why at LEAST 3?
You don't seem to be using 'more' matches.
Or is this just a quick example?


Yes, just a quick example. It came about as a way to detect certain
command line options. A person should be able to abbreviate any
command line option (such that the abbreviation is still uniquely
applicable (e.g. 'deleg' cannot abbreviate 'delete', and if 'define' is
another option then 'de' cannot abbreviate 'delete' nor 'define')).
Normally, I wouldn't use regexes for this. The way you made them is about as
compact as they get for this particular purpose.

if(substr($str,0,3) = substr($needle,0,3){
//code...
}

If you're trying to do what I think you want, maybe this is the code for you:
$needle = 'delete';
preg_match("/^[a-z]+\b/i", $str,$match);
if(strlen($match[0])>=3 && $match[0]==substr($needle,0,strlen($match[0]))){
//code if it matches
}

By no means shorter, but a lot more versatile.


Thanks for the response. It's what I also came up with (with some
lower casing), but it struck me as a lot of code to do something fairly
trivial.

function abbreviates($needle, $haystack) {
// returns the first word in $haystack if it is $needle or
// an abbreviation. Otherwise returns "". Case insensitive
if (!preg_match("/^\\s*(\\w+)\\b/", $haystack, $match)) return "";
return strcasecmp($m=$match[1],substr($needle,0,strlen($m)))
? "" : $m; }

Csaba

Jun 14 '06 #3
Csaba Gabor wrote:
Rik wrote:
Csaba Gabor wrote:

What it seems that you're trying to achieve is similar to REXX's ABBBREV
function. This takes 3 arguments thus:

Abbrev( information, info [, length ] ] )

information -- reference string
The string that may start with the abbreviated text value.

info -- test substring
The abbreviated text value.

length -- minimal substring length
The minimal required length of the test substring -- the default minimum
length is 0 !

Which I would solve like this:

function abbrev($full,$part,$len=0) {
return !(strlen($part)<$len || substr($full,0,strlen($part)) !== $part);
}
Jun 14 '06 #4

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

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
6
by: Oliver Spiesshofer | last post by:
Hi, I would like to replace all strings in a table with regexp: the strings contain the substring "-na", and I would like to replace the whole table field with the original content but without...
20
by: RobG | last post by:
I'm messing with getPropertyValue (Mozilla et al) and currentStyle (IE) and have a general function (slightly modified from one originally posted by Steve van Dongen) for getting style properties:...
19
by: Dr Clue | last post by:
I'm not really an expert with RegExp() , although I do use it. The problem I have is that I want to strip comments out of a CSS file using RegExp() The reason is that I'm loading and parsing to...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
7
by: arno | last post by:
Hi, I want to search a substring within a string : fonction (str, substr) { if (str.search(substr) != -1) { // do something } }
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
9
by: vbfoobar | last post by:
Hello I am looking for python code that takes as input a list of strings (most similar, but not necessarily, and rather short: say not longer than 50 chars) and that computes and outputs the...
4
by: Andrew Poulos | last post by:
I have a string that looks like this "cmi.interactions.fred.id" I need to test that: - the first part of the string is "cmi.interactions." (case sensitive). - the last part of the string is...
4
by: r | last post by:
Hello, It seems delimiters can cause trouble sometimes. Look at this : <script type="text/javascript"> function isDigit(s) { var DECIMAL = '\\.'; var exp = '/(^?0(' + DECIMAL
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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: 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
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
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...

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.