I can replace all occurrences
=SUBSTITUTE("a_b_c_d", "_", "")
to get the string "abcd". Or I can replace the 1st occurrence
=SUBSTITUTE("a_b_c_d", "_", "", 1)
to get the string "ab_c_d". But how can I replace the first X occurrences? I don't know a way to recursively call a function. and =SUBSTITUTE(SUBSTITUTE("a_b_c_d", "_", "", 1), "_", "", 1) is not really an acceptable answer because it would always just replace the first 2 occurrences but what if I need to replace 2 or 3 or 4 or X occurrences, but not all occurrences?
=ARRAYFORMULA(JOIN(,SUBSTITUTE(SPLIT(SUBSTITUTE(A1,"_","_đź’€",3),"đź’€"),{"_",""},"")))
SUBSTITUTE the 3rd occurrence of _ with a skull
SPLIT the given string by the skull
Globally SUBSTITUTE only the first part of splitted string with ""
JOIN them back
Legend:
=ARRAYFORMULA(JOIN(,SUBSTITUTE(SPLIT(SUBSTITUTE(❹,"❶","❶💀",❷),"💀"),{"❶",""},"❸")))
❶search_for
âť·Number of occurrences to be replaced
❸replace_with
âťątext_to_search
Try,
=regexreplace(REGEXEXTRACT(A2, rept("[^_]*_", 2)), "_", text(,))&mid(A2, len(REGEXEXTRACT(A2, rept("[^_]*_", 2)))+1, len(A2))
The 2 the the REPT function that repeats the pattern is the indicator or how many to replace. (in two places)
Linked spreadsheet
Related
I have one word per cell. I need to substitute characters with other characters based on a range of conditions, as follows.
Condition 1 - if the word contains an 'l' double it to 'll'.
Condition 2 - if the first vowel in the word is an 'e', split the word with an apostrophe after said 'e'.
Condition 3 - the last vowel of each word becomes an 'i'.
Condition 4 - if the word ends in 'a','e','i','o', add an m to the end.
Ideally, I'd like them all to work in one formula, but each working separately would suffice. I can apply in a chain, cell to cell.
Condition 1 - SUBSTITUTE(SUBSTITUTE(E2,"l","ll"),"L","Ll")
This is successful.
Condition 2 - SUBSTITUTE("e","e'",1)
Applies to every 'e', rather than only when it is the first vowel in the word.
Together, these work as =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(E2,"l","ll"),"L","Ll"),"e","e'",1)
Condition 3 - NO CURRENT FORMULA
Condition 4 - IF(RIGHT(TRIM(F2),1)="a",F2&"m",F2&"")
Works for a single letter (in this case "a"), but not for all required letters at once.
Use regexreplace(), like this:
=lambda(
data, regexes, replaceWith,
byrow(
data,
lambda(
word,
if(
len(word),
reduce(
trim(word), sequence(counta(regexes)),
lambda(
acc, regexIndex,
regexreplace(
acc,
"(?i)" & index(regexes, regexIndex),
index(replaceWith, regexIndex)
)
)
),
iferror(1/0)
)
)
)
)(
A2:A10,
{ "l", "^([^aeiou]*)(e)", "[aeiou]([^aeiou]*)$", "([aeio])$" },
{ "ll", "$1e-", "i$1", "$1m" }
)
The formula will only deal with lowercase letters because that is what is specified by the question. To replace uppercase letters as well, prefix the first index() with "(?i)" & . Note that case will not be retained.
I want to be able to copy and paste a large string of words from say a text document where there are spaces, returns and not commas between each and every word. Then i want to be able to take out each word individually and put them in a table for example...
input:
please i need help
output:
{1, "please"},
{2, "i"},
{3, "need"},
{4, "help"}
(i will have the table already made with the second column set to like " ")
havent tried anything yet as nothing has come to mind and all i could think of was using gsub to turn spaces into commas and find a solution from there but again i dont think that would work out so well.
Your delimiters are spaces ( ), commas (,) and newlines (\n, sometimes \r\n or \r, the latter very rarely). You now want to find words delimited by these delimiters. A word is a sequence of one or more non-delimiter characters. This trivially translates to a Lua pattern which can be fed into gmatch. Paired with a loop & inserting the matches in a table you get the following:
local words = {}
for word in input:gmatch"[^ ,\r\n]+" do
table.insert(words, word)
end
if you know that your words are gonna be in your locale-specific character set (usually ASCII or extended ASCII), you can use Lua's %w character class for matching sequences of alphanumeric characters:
local words = {}
for word in input:gmatch"%w+" do
table.insert(words, word)
end
Note: The resulting table will be in "list" form:
{
[1] = "first",
[2] = "second",
[3] = "third",
}
(for which {"first", "second", "third"} would be shorthand)
I don't see any good reasons for the table format you have described, but it can be trivially created by inserting tables instead of strings into the list.
I have a string of “words”, like this: fIsh mOuntain rIver. The words are separated by a space, and I added spaces to the beginning and ending of the string to simplify the definition of a “word”.
I need to replace any words containing A, B, or C, with 1, any words containing X, Y, or Z with 2, and all remaining words with 3, e.g.:
the CAT ATE the Xylophone
First, replacing words containing A, B, or C with 1, the string becomes:
the 1 1 the Xylophone
Next, replacing words containing X, Y, or Z with 2, the string becomes:
the 1 1 the 2
Finally, it replaces all remaining words with 3, e.g.:
3 1 1 3 2
The final output is a string containing only numbers, with spaces between.
The words might contain any kind of symbols, e.g.: $5鱼fish can be a word. The only feature defining the beginning and ending of words is the spaces.
The matches are found in order, such that words which might possibly contain two matches, e.g. ZebrA, is simply replaced with 1.
The string is in UTF-8.
How can I replace all of the words containing these particular characters with numbers, and finally replace all remaining words with 3?
Try the following code:
function replace(str)
return (str:gsub("%S+", function(word)
if word:match("[ABC]") then return 1 end
if word:match("[XYZ]") then return 2 end
return 3
end))
end
print(replace("the CAT ATE the Xylophone")) --> 3 1 1 3 2
The slnunicode module provides UTF-8 string functions.
The gsub function/method in Lua is used to replace strings and to check out how times a string is found inside a string. gsub(string old, string from, string to)
local str = "Hello, world!"
newStr, recursions = str:gsub("Hello", "Bye"))
print(newStr, recursions)
Bye, world!    1
newStr being "Bye, world!" because from was change to to and recursions being 1 because "Hello" (from) was only founds once in str.
I'm using iPhone SDK and need of assistance.
I have the following array of search terms:
f, f, last, m
And a string (the top numbers are just indexes for convenience):
0 10 21 30 45
firstname middlename lastname firstnameagain firstnomatch
Expected result ranges (location, length): (0, 1) (30, 1) (21, 4) (10, 1)
I would like to have a regular expression that matches all the search terms in the string but only if they are a prefix of a word. When the search term appears N times, it will match only the first N prefixes (In the example, "f" was entered twice thus the possible match at (45, 1) wasn't returned).
I've tried to write many possible regular expressions and all have failed to match with proper results. I've came to the conclusion that the "\b" meta-character cannot be used because the string can contain non word characters adjacent to letters ("firstname#", "?lastName", ...)
I'm not sure this is a job for a regex alone.
This could be accomplished with or without a regex, by exploding the string by spaces and then checking each string against a list of remaining prefixes.
You keep a list of prefixes as such:
[f, f, last, m]
And when you match against firstname, you remove the prefix that was found. In this case, you remove f:
[f, last, m]
When you match against firstnameagain, you remove f again, and no longer try to match f:
[last, m]
To perform the search, you could iterate through the array of search prefixes and use the NSString rangeOfString function to see if the search prefix is at the front. If you still want to use a regex, you can match against:
#"^(f|f|last|m)"
As you match prefixes, remove them from the array, then remake the regex.
Is there anything better than string.scan(/(\w|-)+/).size (the - is so, e.g., "one-way street" counts as 2 words instead of 3)?
string.split.size
Edited to explain multiple spaces
From the Ruby String Documentation page
split(pattern=$;, [limit]) → anArray
Divides str into substrings based on a delimiter, returning an array
of these substrings.
If pattern is a String, then its contents are used as the delimiter
when splitting str. If pattern is a single space, str is split on
whitespace, with leading whitespace and runs of contiguous whitespace
characters ignored.
If pattern is a Regexp, str is divided where the pattern matches.
Whenever the pattern matches a zero-length string, str is split into
individual characters. If pattern contains groups, the respective
matches will be returned in the array as well.
If pattern is omitted, the value of $; is used. If $; is nil (which is
the default), str is split on whitespace as if ' ' were specified.
If the limit parameter is omitted, trailing null fields are
suppressed. If limit is a positive number, at most that number of
fields will be returned (if limit is 1, the entire string is returned
as the only entry in an array). If negative, there is no limit to the
number of fields returned, and trailing null fields are not
suppressed.
" now's the time".split #=> ["now's", "the", "time"]
While that is the current version of ruby as of this edit, I learned on 1.7 (IIRC), where that also worked. I just tested it on 1.8.3.
I know this is an old question, but this might be useful to someone else looking for something more sophisticated than string.split. I wrote the words_counted gem to solve this particular problem, since defining words is pretty tricky.
The gem lets you define your own custom criteria, or use the out of the box regexp, which is pretty handy for most use cases. You can pre-filter words with a variety of options, including a string, lambda, array, or another regexp.
counter = WordsCounted::Counter.new("Hello, Renée! 123")
counter.word_count #=> 2
counter.words #=> ["Hello", "Renée"]
# filter the word "hello"
counter = WordsCounted::Counter.new("Hello, Renée!", reject: "Hello")
counter.word_count #=> 1
counter.words #=> ["Renée"]
# Count numbers only
counter = WordsCounted::Counter.new("Hello, Renée! 123", rexexp: /[0-9]/)
counter.word_count #=> 1
counter.words #=> ["123"]
The gem provides a bunch more useful methods.
If the 'word' in this case can be described as an alphanumeric sequence which can include '-' then the following solution may be appropriate (assuming that everything that doesn't match the 'word' pattern is a separator):
>> 'one-way street'.split(/[^-a-zA-Z]/).size
=> 2
>> 'one-way street'.split(/[^-a-zA-Z]/).each { |m| puts m }
one-way
street
=> ["one-way", "street"]
However, there are some other symbols that can be included in the regex - for example, ' to support the words like "it's".
This is pretty simplistic but does the job if you are typing words with spaces in between. It ends up counting numbers as well but I'm sure you could edit the code to not count numbers.
puts "enter a sentence to find its word length: "
word = gets
word = word.chomp
splits = word.split(" ")
target = splits.length.to_s
puts "your sentence is " + target + " words long"
The best way to do is to use split method.
split divides a string into sub-strings based on a delimiter, returning an array of the sub-strings.
split takes two parameters, namely; pattern and limit.
pattern is the delimiter over which the string is to be split into an array.
limit specifies the number of elements in the resulting array.
For more details, refer to Ruby Documentation: Ruby String documentation
str = "This is a string"
str.split(' ').size
#output: 4
The above code splits the string wherever it finds a space and hence it give the number of words in the string which is indirectly the size of the array.
The above solution is wrong, consider the following:
"one-way street"
You will get
["one-way","", "street"]
Use
'one-way street'.gsub(/[^-a-zA-Z]/, ' ').split.size
This splits words only on ASCII whitespace chars:
p " some word\nother\tword|word".strip.split(/\s+/).size #=> 4