I have text A B c in a spreadhsheet cell.
How can I remove the whitespaces? =Trim is for leading and closing whitespaces.
=REGEXREPLACE(A1, "\s", "") where A1 is your cell with spaces. It uses a regular expression to match any whitespace character, including spaces, tabs, and newlines. If you only want to remove spaces, then use =REGEXREPLACE(A1, " ", "").
Use the Substitute function like this =SUBSTITUTE(B3, " ", ""). Hope it helps.
Related
I'm using the following ARRAYFORMULA to create an image path:
=ARRAYFORMULA(
if(row(A:A)=1,"#Icon",IF(
B:B="",,SUBSTITUTE(
"../../../../../../_Assets/Icons/"& LOWER(B:B&".png"), " ", "_")
)
)
)
What it does
Adding a path before the text and replaces all spaces with an underscore '_'. Here is an example:
Name
#icon
A Tit(l)e
../../../../../../_Assets/Icons/a_tit(l)e.png
Title - Subtitle
../../../../../../_Assets/Icons/title _-_subtitle.png
Title text/string - Subtitle
../../../../../../_Assets/Icons/title_text/string _-_subtitle.png
What I want it to do
If possible, I would like to achieve the following:
Avoiding/removing characters in the list below like the forward slash / with an underscore _ (see the last row in my example above)
It allready replaces all white spaces with an underscore _ which is good. But when it sees a whitespace followed by a - and another whitespace it will output _-_ but then I want only a -
So the current table above would output the following instead:
Name
#icon
A Tit(l)e
../../../../../../_Assets/Icons/a_tit(l)e.png
Title - Subtitle
../../../../../../_Assets/Icons/title-subtitle.png
Title text/string - Subtitle
../../../../../../_Assets/Icons/title_text_string-subtitle.png
List of characters to be avoided/replaced with an underscore _:
# pound
% percent
& ampersand
{ left curly bracket
} right curly bracket
\ back slash
< left angle bracket
> right angle bracket
* asterisk
? question mark
/ forward slash
blank spaces
$ dollar sign
! exclamation point
' single quotes
" double quotes
: colon
# at sign
+ plus sign
` backtick
| pipe
= equal sign
Any help/suggestion would be much appreciated!
Put list of avoided chars into column and use REGEXREPLACE:
=ARRAYFORMULA(if(row(A:A)=1,"#Icon",IF(A:A="",,"../../../../../../_Assets/Icons/"&LOWER(REGEXREPLACE(REGEXREPLACE(A:A," - ","-"),TEXTJOIN("|\",0,D2:D23),"_")) & ".png")))
try:
=ARRAYFORMULA({"#Icon",
IF(B2:B="",,SUBSTITUTE(SUBSTITUTE(
"../../../../../../_Assets/Icons/"&LOWER(B2:B&".png"), " ", "_"), "_-_", "-", 1))})
I have a column in Google Sheets that has phone numbers with the following formats:
(904) 123 4567
+62 123 4567
+12345678
How can I create another column that automatically removes "+", "(", ")" and spaces to produce 9041234567?
Thanks
You can replace all non-digits with nothing.
=ArrayFormula(regexreplace(A2:A4&"", "\D+",))
Change range to suit and/or use an open-ended range
=ArrayFormula(if(len(A2:A), regexreplace(A2:A4&"", "\D+",),))
You can just use REGEXREPLACE to remove the characters you don't want:
=REGEXREPLACE(A1; "[ \(\)\+]"; "")
The expression used [ \(\)\+], removes the character space, right and left parenthesis and the plus sign. Simply add all other characters you want to remove.
How to extract the values from a csv like string dropping the new lines characters (\r\n or \n) with a pattern.
A line looks like:
1.1;2.2;Example, 3
Notice there are only 3 values and the separator is ;. The problem I'm having is to come up with a pattern that reads the values while dropping the new line characters (the file comes from a windows machine so it has \r\n, reading it from a linux and would like to be independent from the new line character used).
My simple example right now is:
s = "1.1;2.2;Example, 3\r\n";
p = "(.-);(.-);(.-)";
a, b, c = string.match(s, p);
print(c:byte(1, -1));
The two last characters printed by the code above are the \r\n.
The problem is that both, \r and \n are detected by the %c and %s classes (control characters and space characters), as show by this code:
s = "a\r";
print(s:match("%c"));
print(s:match("%s"));
print(s:match("%d"));
So, is it possible to left out from the match the new lines characters? (It should not be assumed that the last two characters will be new lines characters)
The 3ยบ value may contain spaces, punctuation and alphanumeric characters and since \r\n are detected as space characters a pattern like `"(.-);(.-);([%w%s%c]-).*" does not work.
Your pattern
p = "(.-);(.-);(.-)";
does not work: the third field is always empty because .- matches a little as possible. You need to anchor it at the end of the string, but then the third field will contain trailing newline chars:
p = "(.-);(.-);(.-)$";
So, just stop at the first trailing newline char. This also anchors the last match. Try this pattern instead:
p = "(.-);(.-);(.-)[\r\n]";
If trailing newline chars are optional, try this pattern:
p = "(.-);(.-);(.-)[\r\n]*$";
Without any lua experience I found a naive solution:
clean_CR = s:gsub("\r","");
clean_NL = clean_CR:gsub("\n","");
With POSIX regex syntax I'd use
^([^;]*);([^;]*);([^\n\r]*).*$
.. with "\n" and "\r" possibly included as "^M", "^#" (control/unicode characters) .. depending on your editor.
Is there a way to add a space after commas in a string only if it doesn't exist.
Example:
word word,word,word,
Would end up as
word word, word, word,
Is there a function in ruby or rails to do this?
This will be used on hundreds of thousands of sentences, so it needs to be fast (performance would matter).
Using negative lookahead to check no space after comma, then replace with comma and space.
print 'word word,word,word,'.gsub(/,(?![ ])/, ', ')
Just use a regular expression to replace all instances of "," not followed by a space with ", ".
str = "word word,word,word,"
str = str.gsub(/,([^ ])/, ', \1') # "word word, word, word,"
If the string contains no multiple adjacent spaces (or should not contain such), you don't need a regex:
"word word, word, word,".gsub(',', ', ').squeeze(' ')
#=> "word word, word, word, "
Add missing space:
"word word,word,word,".gsub(/,(?=\w)/, ', ') # "word word, word, word,"
and removing the last unnecessary comma if necessary
"word word,word,word,".gsub(/,(?=\w)/, ', ').sub(/,\Z/, '') # "word word, word, word"
I was learning regular expression in iOS, saw this tutorial:http://www.raywenderlich.com/30288/nsregularexpression-tutorial-and-cheat-sheet
It reads like this for \b:
\b matches word boundary characters such as spaces and punctuation. to\b will match the "to" in "to the moon" and "to!", but it will not match "tomorrow". \b is handy for "whole word" type matching.
and \s:
\s matches whitespace characters such as spaces, tabs, and newlines. hello\s will match "hello " in "Well, hello there!".
I have two questions on this:
1) what is the difference between \s and \b? when to use which?
2) \b is handy for "whole word" type matching -> Don't understand the meaning..
Need some guidance on these two.
\b Boundary characters
\b matches the boundary itself but not the boundary character (like a comma or period). It has no length in itself but can be used to find for example e in the end of a word.
For example in the sentence: "Hello there, this is one test. Testing"
The regex e\b will match an e if it's at the end of the word (followed by a word boundary). Notice in the image below that the e in "test" and "Testing" didn't match since the "e" is not followed by a boundary.
\s Whitespace
\s on the other hand matches the actual white space characters (like spaces and tabs). In the same sentence it will match all the spaces between the words.
Edit
Since \b doesn't make much sense alone I showed to how to it as e\b (above). The OP asked (in a comment) about what e\s would match compared to e\b to better explain the difference between \b and \s.
In the same string there is only one match for e\s while there was two matches for e\b since the comma is not a whitespace. Note that the e\s match (image 3) includes the white space where as the e\b match doesn't (image 1).
\b is matching a word boundary. That is a zero width assertion, means it is not matching a character, it is matching a position, where a certain condition is true.
\b is related to \w. \w is defining "word characters", means letters, digits and underscores. So \b is now matching on a change from a word character to a non-word character, or the other way round. Means it matches the start and end of a word, but not the character before or after the word.
\s is a predefined character class that is matching any whitespace character.
See and try out what \bFoo\b matches here on Regexr
See and try out what \sFoo\s matches here on Regexr
\b is zero-width. That is, it doesn't actually match any character. Meanwhile, \s does match a character. This is an important distinction for capturing and more complicated regular expressions.
For example, say you're trying to match numbers that begin with multiple zeros, like 007 or 000101101. You might try:
0+\d*
But see, that would also match 1007 and 101000101101! So then, you might try:
\s0+\d*
But see how that wouldn't match a 007 at the beginning of the string (because there's no space character)? Using \b allows you to get the "whole word (or number)":
\b0+\d*
\b matches any character that is not a letter or number without including itself in the match.
\s matches only white space.
For example:
\b would match any of these: "!?,.##$%^&*()_+ ".
$text = "Hello, Yo! moo .";
$regex = "~o\b~";
^---Will match all three o's.
$text = "Hello, Yo! moo .";
$regex = "~o\s~";
^---Will only match the 'o' in 'moo'.