How to Check If a String Starts with One of Several Prefixes

How to check if a string starts with one of several prefixes?

Do you mean this:

if (newStr4.startsWith("Mon") || newStr4.startsWith("Tues") || ...)

Or you could use regular expression:

if (newStr4.matches("(Mon|Tues|Wed|Thurs|Fri).*"))

Efficient method of checking if a string starts with one of a set of strings

With a compiled regular expression, I would expect the overhead of compiling a regex to pay itself back when you have more than just a few strings. Basically, the compiled regex is an automaton which runs out of traversable paths pretty quickly if the prefix is not one which the automaton recognizes. Especially if all the matches are anchored to the beginning of the string, it should fail very quickly when there is no match.

import re

prefixes = ['foo', 'bar', 'baz']
rx = re.compile(''.join(['^(?:', '|'.join(prefixes), ')']))
for line in input:
match = rx.match(line)
if match:
matched = match.group(0)

If you need a more complex regular expression (say, one with trailing context after the closing parenthesis), you will want to use regular grouping parentheses ( instead of non-grouping (?:, and fetch group(1) instead.

Here is the same with a dictionary mapping prefixes to replacements:

prefixes = {'foo': 'nu', 'bar': 'beer', 'baz': 'base'}
rx = re.compile(''.join(['^(?:', '|'.join(prefixes.keys()), ')']))
for line in input:
match = rx.match(line)
if match:
newval = prefixes[match.group(0)]

Actually, as pointed out in comments, the ^ is not strictly necessary with re.match().

Finding whether a string starts with one of a list's variable-length prefixes

A bit hard to read, but this works:

name=name[len(filter(name.startswith,prefixes+[''])[0]):]

str.startswith with a list of strings to test for

str.startswith allows you to supply a tuple of strings to test for:

if link.lower().startswith(("js", "catalog", "script", "katalog")):

From the docs:

str.startswith(prefix[, start[, end]])

Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for.

Below is a demonstration:

>>> "abcde".startswith(("xyz", "abc"))
True
>>> prefixes = ["xyz", "abc"]
>>> "abcde".startswith(tuple(prefixes)) # You must use a tuple though
True
>>>

How to check if a string begins with prefix and contains specific keyword in Ruby

The code looks good. The only feedback I have is regarding the use of 'include?' function for the prefix. Try to use 'start_with?' function instead, so that you don't get True even when the the "Dr" is within the string.

def match st
if st.start_with?('Dr.') and st.include?('Alex')
return true
else
return false
end
end

How to check if a string starts with another string in C?

Apparently there's no standard C function for this. So:

bool startsWith(const char *pre, const char *str)
{
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? false : memcmp(pre, str, lenpre) == 0;
}

Note that the above is nice and clear, but if you're doing it in a tight loop or working with very large strings, it does not offer the best performance, as it scans the full length of both strings up front (strlen). Solutions like wj32's or Christoph's may offer better performance (although this comment about vectorization is beyond my ken of C). Also note Fred Foo's solution which avoids strlen on str (he's right, it's unnecessary if you use strncmp instead of memcmp). Only matters for (very) large strings or repeated use in tight loops, but when it matters, it matters.

How to use startsWith and str_length simultaneously with multiple prefixes in R

One option is to check the number of characters with nchar, create a logical expression with that, in addition use paste on the 'dx9' by collapsing it to a single pattern string with ^ to specify the start of the string and check with 'DX1' using grepl to return the rows that pass with both logic

subset(yrMonth_ds, nchar(DX1) >=3  & 
grepl(paste0("^(", paste(dx9, collapse="|"), ")"), DX1))
# DX1 ind
#1 8001 0
#4 992 1
#5 1010 0

Check if string begins with one of several substrings in Python

Your problem stems from the fact that string slicing is exclusive of the stop index:

In [7]: line = '0123456789'

In [8]: line[0:3]
Out[8]: '012'

In [9]: line[0:4]
Out[9]: '0123'

In [10]: line[:3]
Out[10]: '012'

In [11]: line[:4]
Out[11]: '0123'

Slicing a string between i and j returns the substring starting at i, and ending at (but not including) j.

Just to make your code run faster, you might want to test membership in sets, instead of in lists:

cleanLines = []
line = "sample input here"
blacklist = set(["node", "path", "Path"])
if line[:4] not in blacklist: #skip standard headers
cleanLines.append(line)

Now, what you're actually doing with that code is a startswith, which is not restricted by any length parameters:

In [12]: line = '0123456789'

In [13]: line.startswith('0')
Out[13]: True

In [14]: line.startswith('0123')
Out[14]: True

In [15]: line.startswith('03')
Out[15]: False

So you could do this to exclude headers:

cleanLines = []
line = "sample input here"
headers = ["node", "path", "Path"]
if not any(line.startswith(header) for header in headers) : #skip standard headers
cleanLines.append(line)


Related Topics



Leave a reply



Submit