remove text after final period in string
Here are a few solutions:
sub("^(.*)[.].*", "\\1", "abc.com.foo.bar") # 1
## [1] "abc.com.foo"
library(tools)
file_path_sans_ext("abc.com.foo.bar") # 3
## [1] "abc.com.foo"
ADDED. Regarding your comment asking to remove leading periods, simplest is to just feed this into any of the above where x
is the input string:
sub("^[.]*", "", x)
To do any of them in one line:
x <- c("abc.com.foo.bar", ".abc.com.foo.bar", ".vimrc")
sub("^[.]*(.*)[.]?.*$", "\\1", x) # 1a
## [1] "abc.com.foo.bar" "abc.com.foo.bar" "vimrc"
file_path_sans_ext(sub("^[.]*", "", x))
## [1] "abc.com.foo" "abc.com.foo" "vimrc"
remove initial period and text after final period in string
This regex does what you want:
^\.+|\.[^.]*$
Replace its matches with the empty string.
In R:
gsub("^\\.+|\\.[^.]*$", "", subject, perl=TRUE);
Explanation:
^ # Anchor the match to the start of the string
\.+ # and match one or more dots
| # OR
\. # Match a dot
[^.]* # plus any characters except dots
$ # anchored to the end of the string.
Remove characters after specific character in string, then remove substring?
For string manipulation, if you just want to kill everything after the ?, you can do this
string input = "http://www.somesite.com/somepage.aspx?whatever";
int index = input.IndexOf("?");
if (index >= 0)
input = input.Substring(0, index);
Edit: If everything after the last slash, do something like
string input = "http://www.somesite.com/somepage.aspx?whatever";
int index = input.LastIndexOf("/");
if (index >= 0)
input = input.Substring(0, index); // or index + 1 to keep slash
Alternately, since you're working with a URL, you can do something with it like this code
System.Uri uri = new Uri("http://www.somesite.com/what/test.aspx?hello=1");
string fixedUri = uri.AbsoluteUri.Replace(uri.Query, string.Empty);
How would I remove the text before the initial period, the initial period itself and text after final period in a string?
One option in base R
is to capture as a group ((...)
) the word followed by the dot (\\.
) and the word (\\w+
) till the end ($
) of the string. In the replacement, use the backreference (\\1
) of the captured word
sub(".*\\.(\\w+)\\.\\w+$", "\\1", str1)
#[1] "IJKL"
Here, we match characters (.*
) till the .
(\\.
- escaped to get the literal value because .
is a metacharacter that will match any character if not escaped), followed by the word captured ((\\w+)
), followed by a dot and another word at the end ($
)of the string. The replacement part is mentioned above
Or another option is regmatches/regexpr
from base R
regmatches(str1, regexpr("\\w+(?=\\.\\w+$)", str1, perl = TRUE))
#[1] "IJKL"
Or another option is word
from stringr
library(stringr)
word(str1, -2, sep="[.]")
#[1] "IJKL"
data
str1 <- "ABCD.EF.GH.IJKL.MN"
Remove all text after last occurrence of a substring in MySQL
You can use
SELECT
CASE
WHEN INSTR(school, " - ") > 0 THEN
LEFT(school, CHAR_LENGTH(school) - LOCATE(" - ", REVERSE(school))-2)
ELSE
school
END
As RESULT FROM table_name;
See the online demo.
Testing against
create table table_name (school varchar(320), ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
insert into table_name (school) values ("Associated Hebrew Schools of Toronto - Central Administration - North York");
insert into table_name (school) values ("Associated Hebrew Schools of Toronto");
yields
Also, if you are using MySQL v.8+, you can use REGEXP_REPLACE
:
create table table_name (school varchar(320), ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
insert into table_name (school) values ("Associated Hebrew Schools of Toronto - Central Administration - North York");
insert into table_name (school) values ("Associated Hebrew Schools of Toronto - North York");
insert into table_name (school) values ("Associated Hebrew Schools of Toronto");
SELECT REGEXP_REPLACE(school, "(.*)\\s-\\s.*", "$1") As RESULT FROM table_name;
Here, (.*)\s-\s.*
matches and captures into Group 1 any text up to [whitespace]-[whitespace], then the latter is matched and the .*
matches and consumes the rest of the string; the $1
replacement puts back the Group 1 value. See this online DB fiddle yielding
Remove characters after the last occurrence of a specific character
Figured it out!
outcome <- sub("_[^_]+$", "", exampleList)
How to remove all characters after a specific character in python?
Split on your separator at most once, and take the first piece:
sep = '...'
stripped = text.split(sep, 1)[0]
You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.
Remove everything after last backslash
You need lastIndexOf
and substr
...
var t = "\\some\\route\\here";
t = t.substr(0, t.lastIndexOf("\\"));
alert(t);
Also, you need to double up \
chars in strings as they are used for escaping special characters.
Update
Since this is regularly proving useful for others, here's a snippet example...
// the original string
var t = "\\some\\route\\here";
// remove everything after the last backslash
var afterWith = t.substr(0, t.lastIndexOf("\\") + 1);
// remove everything after & including the last backslash
var afterWithout = t.substr(0, t.lastIndexOf("\\"));
// show the results
console.log("before : " + t);
console.log("after (with \\) : " + afterWith);
console.log("after (without \\) : " + afterWithout);
Related Topics
R Subset with Condition Using %In% or ==. Which One Should Be Used
Filtering Rows in R Unexpectedly Removes Nas When Using Subset or Dplyr::Filter
Find Names of Columns Which Contain Missing Values
Using Geom_Rect for Time Series Shading in R
What Does the Error "Arguments Imply Differing Number of Rows: X, Y" Mean
How to Format Data for Plotly Sunburst Diagram
Rounding Time to Nearest Quarter Hour
Format Text Inside R Code Chunk
Got Message Unable to Load Shared Object Stats.So When R Starts
How to Jitter Both Geom_Line and Geom_Point by the Same Magnitude
Deleting Specific Rows from a Data Frame
Remove "Showing 1 to N of N Entries" Shiny Dt
Aligning Data Frame with Missing Values
Data.Table Is Not Handling Integer64 in by Statement