Remove All Text Before Colon

Remove all text before colon

Here are two ways of doing it in R:

foo <- "TF_list_to_test10004/Nus_k0.345_t0.1_e0.1.adj:PKMYT1"

# Remove all before and up to ":":
gsub(".*:","",foo)

# Extract everything behind ":":
regmatches(foo,gregexpr("(?<=:).*",foo,perl=TRUE))

Regex: How to remove everything before a colon on each line

If you want to make sure you do not overflow across lines, you need to add \r\n to the negated character class and replace \s that can match line breaks with \h (horizontal whitespace pattern) (or [ \t] if \h is not supported).

So, you might use

^[^:\r\n]+:\h*

(see demo) or

^[^:\r\n]+:[ \t]*

to replace with an empty string (another demo).

In Notepad++, you need to match the whole line to get rid of the recursive behavior:

^[^:\r\n]+:\h*(.*)

Replace with \1. See yet another regex demo.

Pattern details

  • ^ - start of a line (if not default, prepend with (?m) inline modifier)
  • [^:\r\n]+ - 1 or more chars other than :, CR and LF
  • : - a colon
  • \h* - zero or more horizontal whitespaces
  • (.*) - Group 1 (referred to with \1 or $1 from the replacement pattern) capturing any zero or more chars other than line break chars, as many as possible (up to the end of line).

How to remove everything before colon in a dataframe

pandas.DataFrame.applymap() is also intuitive.

df = df.applymap(lambda x: str(x).split(':')[-1])
# print(df)

0 1 2 3 4 5 6
0 2 -0.860107 -0.111111 -1 -1 -1 -0.777778
1 2 -0.859671 -0.111111 -0.333333 -0.333333 -0.111111 0.333333
2 2 -0.857807 -0.555556 -1 -1 -1 -0.777778

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).

How to remove all characters before a specific character in Python?

Use re.sub. Just match all the chars upto I then replace the matched chars with I.

re.sub(r'^.*?I', 'I', stri)

Extract text before the fullstop or colon

Or:

library(stringr)

city <- c('Kirkland-1234.It is a goodtown','Bethesda-345. small town', 'Wellington: 12345')

str_extract(city, "^[^\\.:]+")

#1] "Kirkland-1234" "Bethesda-345" "Wellington"


Related Topics



Leave a reply



Submit