Insert bracket before a =TEXT() - excel

I have a function that shows a begin- and end date. I wanted to have it between brackets in a cell:
=TEXT(A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
I have the outher bracket already:
Example ->01/01/2017 - 02/03/2017)
But can't seem to try around and insert a bracket in the beginning.
I have tried:
=TEXT((A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
=TEXT("("A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
=TEXT(&"("A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
"("=TEXT(A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
Why isn't this working?

Use the concatenate function.
Try this: (Convention Changed)
=concatenate("("; TEXT(A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj"); ")")

You can avoid string concatenation by making the extra characters part of the format mask used by the TEXT function. When using reserved characters or symbols that a format mask usually has another purpose for, precede them with a backslash to convert them to a 'string literal'.
=TEXT(A1; "\(dd/mm/jjjj - ")&TEXT(B1; "dd/mm/jjjj\)")

Related

Strip characters to the left of a specific character in a pandas column

I have the following data:
key German
0 0:- Profile 1
1 1:- Archetype Realist*in
2 2:- RIASEC Code: R- Realistic
3 3:- Subline Deine Stärke? Du bleibst dir selber treu.
4 4:- Copy Dein Erfolg basiert auf deiner praktischen Ver...
In the "Key" column I would like to remove the numbers and colon dash which follows. This order is always the same (from the left). So for the first row I would like to remove "0:- ", and just leave "Profile 1". I am struggling to find the correct regex expression to do what I want. Originally I tried the following:
df_json['key'] = df_json['key'].map(lambda x: x.strip(':- ')[1])
However, this approach is too restrictive since there can be multiple words in the field.
I would like to use pd.Series.str.replace(), but I cant figure out the correct regex expression to achieve the desired results. Any help would be greatly appreciated.
With your shown samples, please try following. Using replace function of Pandas here. Simple explanation would be, apply replace function of Pandas to German column of dataframe and then use regex ^[0-9]+:-\s+ to replace values with NULL.
df['German'].replace('(^[0-9]+:-\s+)','', regex=True)
Explanation:
^[0-9]+: match starting digits followed by colon here.
:-\s+: Match colon, followed by - followed by 1 or more space occurrences.
What about just using pandas.Series.str.partition instead of regular expressions:
df['German'] = df['German'].str.partition()[2]
This would split the series on the 1st space only and grab the trailing part. Alternatively to partition you could also just split:
df['German'] = df['German'].str.split(' ', 1).str[1]
If regex is a must for you, maybe use a lazy quantifier to match upto the 1st space character:
df['German'] = df['German'].replace('^.*? +','', regex=True)
Where:
^ - Start line anchor.
.*? - Any 0+ (lazy) characters other than newline upto;
+ - 1+ literal space characters.
Here is an online demo
You need
df_json['key'] = df_json['key'].str.replace(r'^\d+:-\s*', '', regex=True)
See the regex demo and the regex graph:
Details:
^ - start of string
\d+ - one or more digits
: - a colon
- - a hyphen
\s* - zero or more whitespaces
Extract any non white Space \S and Non Digits \D which are immediately to the left of unwanted characters
df['GermanFiltered']=df['German'].str.extract("((?<=^\d\:\-\s)\S+\D+)")

Batch: Replacing just the first character in a string if it is a certain character

I have several datalines like so:
v1.4.00.29
- SP.CNG v1.0.2.2
Update Kit - Secure USB Token v1.1.1.1
- HI_3997 v1.0.3997.1
- HI_4009 v1.0.4009.1
- HI_3585 v1.0.3585
Update Kit - RM4 v1.0.1202.4
Update Kit - DN Series v1.0.4.1
Is there some easy way to check if the first character is a - and then delete this PLUS the space next to them so that the line is aligned to the other lines.
My first try was just to delete the -and spacesresulting in a not looking result as ALL - are replaced:
set tmp=!tmp:-=!
set tmp=!tmp: =!
You can do this using the "substring" method of referring to environment variables. In brief:
%TMP:~n,m%
will extract m characters from TMP, starting with the nth, where counting starts from zero. (You can also omit ,m to get "the rest of the string" and use negative numbers to mean "from the end of the string" – see the output of SET /? for more details).
In your case, something like the following should work:
if "%TMP:~0,2%" == "- " set "TMP=%TMP:~2%"
This checks whether the first two characters are a minus and a space (you could relax this to just checking the first character if needed). If there's a match, it replaces TMP with all characters starting with the third (0=1st, 1=2nd etc.).
You might want to try the following code that uses sub-string substitution (the string to process is stored in variale tmp):
if "- !tmp:*- !"=="!tmp!" set "tmp=!tmp:*- =!"
This removes everything up to and including the first occurrence of - + SPACE from the string and precedes the result with - + SPACE; if this is now equal to the original string then the first - + SPACE must be at the beginning, so remove it; otherwise do not alter the string.

excel trim function is removing spaces in middle of text - this was unexpected (?)

The excel trim function is removing spaces in middle of text - this was unexpected (?)
i.e. I thought that the excel trim was for trimming leading and trailing spaces.
e.g. a cell value of =Trim("Last Obs Resp") becomes a value of "Last Obs Resp"
Sure enough Microsoft documents it this way:
https://support.office.com/en-gb/article/trim-function-410388fa-c5df-49c6-b16c-9e5630b479f9
I am used to the Oracle database trim function which only removes leading and trailing spaces.
https://www.techonthenet.com/oracle/functions/trim.php
Was excel Trim function always this way?
Excel does not have ltrim and rtrim functions..
i.e. I can't do:
=RTRIM(Ltrim("Last Obs Resp"))
I wonder how I achieve the equivalent in Excel when I don't want to remove doubled up spaces in the middle of the string?
This page documents VBA trim function:
https://www.techonthenet.com/excel/formulas/trim.php
Create a UDF that uses VBA's version of Trim which does not touch the inner spaces. Only removing the leading and trailing spaces
Function MyTrim(str As String) As String
MyTrim = Trim(str)
End Function
Then you can call it from the worksheet:
=MyTrim(A1)
If you want a formula to do it:
=MID(LEFT(A1,AGGREGATE(14,6,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1)))/(MID(A1,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1))),1)<>" "),1)),AGGREGATE(15,6,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1)))/(MID(A1,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1))),1)<>" "),1),999)

How do I delete text before the last forward slash in a column?

I have a spreadsheet with columns that show a filepath. They look like this:
/j/t/jtfdsrn-01r_1_1_19.jpg
/j/t/jtfdsrn-01r_1_1_18.jpg
/j/t/jtfdsrn-01r_1_1_17.jpg
/j/t/jtfdsrn-01r_1_1_16.jpg
/j/t/jtfdsrn-01r_1_1_15.jpg
/j/t/jtfdsrn-01r_1_1_14.jpg
/j/t/jtfdsrn-01r_1_1_13.jpg
/j/t/jtfdsrn-01r_1_1_12.jpg
I want to remove everything before the last slash so they look like this:
/jtfdsrn-01r_1_1_19.jpg
/jtfdsrn-01r_1_1_18.jpg
/jtfdsrn-01r_1_1_17.jpg
/jtfdsrn-01r_1_1_16.jpg
/jtfdsrn-01r_1_1_15.jpg
/jtfdsrn-01r_1_1_14.jpg
/jtfdsrn-01r_1_1_13.jpg
/jtfdsrn-01r_1_1_12.jpg
Can I do this with a formula or an built-in function? I use OpenOffice.
I have tried the TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",LEN(A1))),LEN(A1))) formula but I get a Error:501 on it.
If your target string is always the same length:
=RIGHT(A1,23)
Input: /j/t/jtfdsrn-01r_1_1_19.jpg Output: /jtfdsrn-01r_1_1_19.jpg
If you have variable length strings and always 3 backslashes in filepath:
="/" &RIGHT(A1, LEN(A1) -FIND("*", SUBSTITUTE(A1,"/","*",3), 1))
Input: /j/t/jtfdsrn-01r_1_1_1000.jpg Output: /jtfdsrn-01r_1_1_1000.jpg
If you have variable length strings and variable backslashes in filepath:
="/" &RIGHT(A1, LEN(A1) -FIND("*", SUBSTITUTE(A1,"/","*", LEN(A1)-LEN(SUBSTITUTE(A1,"/","") )), 1))
Input: a/b/c/j/t/jtfdsrn-01r_1_1_19.jpg Output: /jtfdsrn-01r_1_1_19.jpg
Let me know if this works for you if your values dont change there shouldnt be an issue but if so ill look into it.
=RIGHT(A2;LEN(A2)-FIND("/";A2;3)-1)
Simply Use Wildcard (*/) Without Brackets In Find and Replace

How can I perform a reverse string search in Excel without using VBA?

I have an Excel spreadsheet containing a list of strings. Each string is made up of several words, but the number of words in each string is different.
Using built in Excel functions (no VBA), is there a way to isolate the last word in each string?
Examples:
Are you classified as human? -> human?
Negative, I am a meat popsicle -> popsicle
Aziz! Light! -> Light!
This one is tested and does work (based on Brad's original post):
=RIGHT(A1,LEN(A1)-FIND("|",SUBSTITUTE(A1," ","|",
LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))
If your original strings could contain a pipe "|" character, then replace both in the above with some other character that won't appear in your source. (I suspect Brad's original was broken because an unprintable character was removed in the translation).
Bonus: How it works (from right to left):
LEN(A1)-LEN(SUBSTITUTE(A1," ","")) – Count of spaces in the original string
SUBSTITUTE(A1," ","|", ... ) – Replaces just the final space with a |
FIND("|", ... ) – Finds the absolute position of that replaced | (that was the final space)
Right(A1,LEN(A1) - ... )) – Returns all characters after that |
EDIT: to account for the case where the source text contains no spaces, add the following to the beginning of the formula:
=IF(ISERROR(FIND(" ",A1)),A1, ... )
making the entire formula now:
=IF(ISERROR(FIND(" ",A1)),A1, RIGHT(A1,LEN(A1) - FIND("|",
SUBSTITUTE(A1," ","|",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))))
Or you can use the =IF(COUNTIF(A1,"* *") syntax of the other version.
When the original string might contain a space at the last position add a trim function while counting all the spaces: Making the function the following:
=IF(ISERROR(FIND(" ",B2)),B2, RIGHT(B2,LEN(B2) - FIND("|",
SUBSTITUTE(B2," ","|",LEN(TRIM(B2))-LEN(SUBSTITUTE(B2," ",""))))))
This is the technique I've used with great success:
=TRIM(RIGHT(SUBSTITUTE(A1, " ", REPT(" ", 100)), 100))
To get the first word in a string, just change from RIGHT to LEFT
=TRIM(LEFT(SUBSTITUTE(A1, " ", REPT(" ", 100)), 100))
Also, replace A1 by the cell holding the text.
A more robust version of Jerry's answer:
=TRIM(RIGHT(SUBSTITUTE(TRIM(A1), " ", REPT(" ", LEN(TRIM(A1)))), LEN(TRIM(A1))))
That works regardless of the length of the string, leading or trailing spaces, or whatever else and it's still pretty short and simple.
I found this on google, tested in Excel 2003 & it works for me:
=IF(COUNTIF(A1,"* *"),RIGHT(A1,LEN(A1)-LOOKUP(LEN(A1),FIND(" ",A1,ROW(INDEX($A:$A,1,1):INDEX($A:$A,LEN(A1),1))))),A1)
[edit] I don't have enough rep to comment, so this seems the best place...BradC's answer also doesn't work with trailing spaces or empty cells...
[2nd edit] actually, it doesn't work for single words either...
=RIGHT(TRIM(A1),LEN(TRIM(A1))-FIND(CHAR(7),SUBSTITUTE(" "&TRIM(A1)," ",CHAR(7),
LEN(TRIM(A1))-LEN(SUBSTITUTE(" "&TRIM(A1)," ",""))+1))+1)
This is very robust--it works for sentences with no spaces, leading/trailing spaces, multiple spaces, multiple leading/trailing spaces... and I used char(7) for the delimiter rather than the vertical bar "|" just in case that is a desired text item.
This is very clean and compact, and works well.
{=RIGHT(A1,LEN(A1)-MAX(IF(MID(A1,ROW(1:999),1)=" ",ROW(1:999),0)))}
It does not error trap for no spaces or one word, but that's easy to add.
Edit:
This handles trailing spaces, single word, and empty cell scenarios. I have not found a way to break it.
{=RIGHT(TRIM(A1),LEN(TRIM(A1))-MAX(IF(MID(TRIM(A1),ROW($1:$999),1)=" ",ROW($1:$999),0)))}
=RIGHT(A1,LEN(A1)-FIND("`*`",SUBSTITUTE(A1," ","`*`",LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))
New answer 9/28/2022
Considering the new excel function: TEXTAFTER (check availability) you can achieve it with a simple formula:
=TEXTAFTER(A1," ", -1)
To add to Jerry and Joe's answers, if you're wanting to find the text BEFORE the last word you can use:
=TRIM(LEFT(SUBSTITUTE(TRIM(A1), " ", REPT(" ", LEN(TRIM(A1)))), LEN(SUBSTITUTE(TRIM(A1), " ", REPT(" ", LEN(TRIM(A1)))))-LEN(TRIM(A1))))
With 'My little cat' in A1 would result in 'My little' (where Joe and Jerry's would give 'cat'
In the same way that Jerry and Joe isolate the last word, this then just gets everything to the left of that (then trims it back)
Copy into a column, select that column and HOME > Editing > Find & Select, Replace:
Replace All.
There is a space after the asterisk.
Imagine the string could be reversed. Then it is really easy. Instead of working on the string:
"My little cat" (1)
you work with
"tac elttil yM" (2)
With =LEFT(A1;FIND(" ";A1)-1) in A2 you get "My" with (1) and "tac" with (2), which is reversed "cat", the last word in (1).
There are a few VBAs around to reverse a string. I prefer the public VBA function ReverseString.
Install the above as described. Then with your string in A1, e.g., "My little cat" and this function in A2:
=ReverseString(LEFT(ReverseString(A1);IF(ISERROR(FIND(" ";A1));
LEN(A1);(FIND(" ";ReverseString(A1))-1))))
you'll see "cat" in A2.
The method above assumes that words are separated by blanks. The IF clause is for cells containing single words = no blanks in cell. Note: TRIM and CLEAN the original string are useful as well. In principle it reverses the whole string from A1 and simply finds the first blank in the reversed string which is next to the last (reversed) word (i.e., "tac "). LEFT picks this word and another string reversal reconstitutes the original order of the word (" cat"). The -1 at the end of the FIND statement removes the blank.
The idea is that it is easy to extract the first(!) word in a string with LEFT and FINDing the first blank. However, for the last(!) word the RIGHT function is the wrong choice when you try to do that because unfortunately FIND does not have a flag for the direction you want to analyse your string.
Therefore the whole string is simply reversed. LEFT and FIND work as normal but the extracted string is reversed. But his is no big deal once you know how to reverse a string. The first ReverseString statement in the formula does this job.
=LEFT(A1,FIND(IF(
ISERROR(
FIND("_",A1)
),A1,RIGHT(A1,
LEN(A1)-FIND("~",
SUBSTITUTE(A1,"_","~",
LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))
)
)
)
),A1,1)-2)
I translated to PT-BR, as I needed this as well.
(Please note that I've changed the space to \ because I needed the filename only of path strings.)
=SE(ÉERRO(PROCURAR("\",A1)),A1,DIREITA(A1,NÚM.CARACT(A1)-PROCURAR("|", SUBSTITUIR(A1,"\","|",NÚM.CARACT(A1)-NÚM.CARACT(SUBSTITUIR(A1,"\",""))))))
Another way to achieve this is as below
=IF(ISERROR(TRIM(MID(TRIM(D14),SEARCH("|",SUBSTITUTE(TRIM(D14)," ","|",LEN(TRIM(D14))-LEN(SUBSTITUTE(TRIM(D14)," ","")))),LEN(TRIM(D14))))),TRIM(D14),TRIM(MID(TRIM(D14),SEARCH("|",SUBSTITUTE(TRIM(D14)," ","|",LEN(TRIM(D14))-LEN(SUBSTITUTE(TRIM(D14)," ","")))),LEN(TRIM(D14)))))
You can achieve this also by reversing the string and finding the first space
=MID(C3,2+LEN(C3)-SEARCH(" ",CONCAT(MID(C3,SEQUENCE(LEN(C3),,LEN(C3),-1),1))),LEN(A1))
Reverse the string
CONCAT(MID(C3,SEQUENCE(LEN(C3),,LEN(C3),-1),1))
Find the first space in the reversed string
SEARCH(" ",...
Take the position of the space found in the reversed string off the length of the string and return that portion
=MID(C3,2+LEN(C3)-SEARCH...
I also had a task like this and when I was done, using the above method, a new method occured to me: Why don't you do this:
Reverse the string ("string one" becomes "eno gnirts").
Use the good old Find (which is hardcoded for left-to-right).
Reverse it into readable string again.
How does this sound?

Resources