Deleting string up to the first occurrence of certain character
You could use cut
:
$ cut -d":" -f2- myfile.txt
remove everything up to first occurrence of special characters
This works by using the ?
of regex.
Full regex break down:
^
the start of the string.+
match any character unlimited times?
Matches the preceding expression 0 or 1 times(\.\.\.)
...
where the.
is escaped by\
the last space of your regex
then use String.prototype.replace
to replace the set described by regex with an empty string.
See the regex here.
let str = `28 Mar 2017 ... helloo ... i need to remove everything until first occurrence of special charaters ...`
console.log(str.replace(/^.+?(\.\.\.) /, ''))
Remove first occurrence of comma in a string
This will do it:
if (str.match(/,.*,/)) { // Check if there are 2 commas
str = str.replace(',', ''); // Remove the first one
}
When you use the replace
method with a string rather than an RE, it just replaces the first match.
How to remove the contents of a string after the first occurrence of a character in java?
I think replaceFirst("[-+.^:,_]\d.*", "")
should do what you want.
You can read that as "a 'symbol' followed by a digit followed by everything else in the string."
remove the word in a string before first occurrence of hyphen or underscore in R
Here is a solution using the dplyr
and stringr
packages.
string_2
regular expression
removes up until the first _
or -
and is replaced with MYD
.
string_3
regular expression
removes through the first _
or -
.
library(dplyr)
library(stringr)
df <- tibble(string_1 = c("Sec_GHTY_WE", "NewSec_JOL_ru", "Sec-KIH-YRK", "Sec_PWq-FTF", "NewSec-LPO-WE"))
df %>%
mutate(
string_2 = str_replace(string_1, pattern = "^.*?(?=-|_)", "MYD"),
string_3 = str_remove(string_1, pattern = "^.*?(_|-)")
)
#> # A tibble: 5 x 3
#> string_1 string_2 string_3
#> <chr> <chr> <chr>
#> 1 Sec_GHTY_WE MYD_GHTY_WE GHTY_WE
#> 2 NewSec_JOL_ru MYD_JOL_ru JOL_ru
#> 3 Sec-KIH-YRK MYD-KIH-YRK KIH-YRK
#> 4 Sec_PWq-FTF MYD_PWq-FTF PWq-FTF
#> 5 NewSec-LPO-WE MYD-LPO-WE LPO-WE
Created on 2020-11-16 by the reprex package (v0.3.0)
How to remove anything that starts with specific character (@) in various locations in string in R?
We may change the pattern to match zero or more space (\\s*
) followed by @
and one or more non-white space (\\S+
) in str_remove_all
to remove those substring
library(stringr)
library(dplyr)
Customer %>%
mutate(Cleaned_Tweet = str_remove_all(Tweet, "\\s*@\\S+"))
-output
ID Tweet Cleaned_Tweet
1 1 @ChipotleTweets @ChipotleTweets Becky is very nice Becky is very nice
2 2 Happy Halloween! I now look forward to $3 booritos at @ChipotleTweets Happy Halloween! I now look forward to $3 booritos at
3 3 Considering walking to @.ChipotleTweets in my llama onesie. Considering walking to in my llama onesie.
NOTE: str_remove
just removes the first instance of match i.e. if there are more than one match in a single string, it skips the others and matches only the first. We need str_remove_all
for removing all instances of matching patterns.
data
Customer <- structure(list(ID = 1:3, Tweet = c("@ChipotleTweets @ChipotleTweets Becky is very nice",
"Happy Halloween! I now look forward to $3 booritos at @ChipotleTweets",
"Considering walking to @.ChipotleTweets in my llama onesie."
)), class = "data.frame", row.names = c(NA, -3L))
Replace the first occurrence of a character within a string in R
The g
in gsub
stands for global, indicating that it will match all occurrences. If you use sub
instead of gsub
, only the first occurrence will be matched and replaced. See ?gsub
for details, in the Description:
sub
andgsub
perform replacement of the first and all matches respectively.
And, if you only want to replace the colon, your pattern should just be ":"
, ".*:"
will match and replace everything up through the last colon. If you want to replace everything up through the first colon, using sub
and ?
to make *
not greedy will work.
x = "Request(123): \n Element1: 123123 \n Element2: 456456"
## match everything up through last colon
sub(".*:", "", x)
# [1] " 456456"
## not greedy, match everything up through first colon
sub(".*?:", "", x)
# [1] " \n Element1: 123123 \n Element2: 456456"
## match first colon only
## since we don't need regex here, fixed = TRUE will speed things up
sub(":", "", x, fixed = TRUE)
#[1] "Request(123) \n Element1: 123123 \n Element2: 456456"
## compare to gsub, match every colon
gsub(":", "", x, fixed = TRUE)
# [1] "Request(123) \n Element1 123123 \n Element2 456456"
Removing the first occurrence of a string from another string in Python
Refactoring your code
def remove(substring, string):
sublen = len(substring)
new_string = ""
for i in range(len(string)):
if string[i:i+sublen] == substring:
# found so append remaining string
new_string += string[i+sublen:]
break
else:
new_string += string[i] # not found so append character
return new_string
print(remove("today", 'today is friday, the last friday of the month'))
Output
is friday, the last friday of the month
Alternate Solution (from your link)
def remove(substring, string)
return string.replace(substring, '', 1)
This uses the replace
function to replace the first occurrence of a string.
Python String | replace provides a good description of how this works.
Removing beginning of a string until the first instance of a specific character
If you can rely on -
preceding the name and you're not adamant on using a regex:
s = '| | +-out\windows-x86-MD-mbcs-vs2008-rel\bin\VisualStudio08-32bit.exe'
print s.split('-', 1)[1]
Output: out\windows-x86-MD-mbcs-vs2008-relin\VisualStudio08-32bit.exe
How to remove all characters before a specific character in Java?
You can use .substring()
:
String s = "the text=text";
String s1 = s.substring(s.indexOf("=") + 1);
s1.trim();
then s1
contains everything after =
in the original string.
s1.trim()
.trim()
removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).
Related Topics
Linux Newbie: Linux VS Posix Manual
In Bash, Why 'X=100 Echo $X' Doesn't Print Anything
How to Use Global Arrays in Bash
How to Diff Directories Over Ssh
How to Break Up an Extremely Long String Literal in Bash
Matlab Execute Script from Linux Command Line
Detecting a Chroot Jail from Within
What Is the Pid in the Host, of a Process Running Inside a Docker Container
How to Make One Linux Kernel Module Depend on Another External Module with Depmod
Git Diff with Line Numbers and Proper Code Alignment/Indentation
Bash Script to Find and Display Oldest File
Does Os X Have an Equivalent to /Dev/Shm
Abuse Curl to Communicate with Redis