<p>Without <em>regex</em>, split on <code>"_"</code>, drop last item, then <em>paste</em> it back again:</p>

<pre><code>sapply(strsplit(x, "_"), function(i) paste(head(i, -1), collapse = "_"))
</code></pre>

<p>Or with <em>regex</em> (stealing the code from <code>tools::file_path_sans_ext</code> function which is used to drop file extensions):</p>

<pre><code>sub("([^_]+)\\_[[:alnum:]]+$", "\\1", x)

# [1] "BAF60A_KD_1"     "BAF60A_KD_2"     "BAF60A_RA_1"     "BAF60A_RA_2"     "BAF60A_VD3_1"    "BAF60A_VD3_2"   
# [7] "BAF60C_KD_1_UI"  "BAF60C_KD_2_UI"  "BAF60C_KD_RA_1"  "BAF60C_KD_RA_2"  "BAF60C_KD_VD3_1" "BAF60C_KD_VD3_2"
# [13] "VC_1_UI"         "VC_2_UI"         "VC_RA1"          "VC_RA_2"         "VC_VD3_1"        "VC_VD3_2" 
</code></pre>

CC BY-SA 4.0
2019-04-04T12:27:24.150Z
multiple ways of doing wow
kcm
CC BY-SA 4.0
2019-04-04T12:32:19.407Z
@krushnachChandra minor note, you will get a quicker and better answers if you post regex/R posts at StackOverflow.
zx8754
CC BY-SA 4.0
2019-04-04T12:36:51.697Z
well actually i got banned from posting it on the R forum ,i guess i asked stupid questions.it says from this account no more questions.But im glad for multiple answers coming out
kcm
CC BY-SA 4.0
2019-04-04T12:38:27.633Z
What do you mean "banned"? This is your profile, right? https://stackoverflow.com/users/6342356/krushnach-chandra
zx8754
CC BY-SA 4.0
2019-04-04T12:54:12.670Z
yes i do ask question on this forum and other forum but not in the R section may be bad questions
kcm
CC BY-SA 4.0
2019-04-04T12:55:25.400Z
This is what i see when i try to ask question on that forum "You have reached your question limit
Sorry, we are no longer accepting questions from this account. See the Help Center to learn more."
kcm
CC BY-SA 4.0
2019-04-05T12:24:52.007Z
@krushnachChandra Looks like you have a system ban "for asking poor questions", it is temporary, not sure for how long. Relevant meta post regarding ban: https://meta.stackexchange.com/q/86997/228487
zx8754
CC BY-SA 4.0
2019-04-05T12:53:07.983Z
okay no issues..i as quite naive to ask certain question ..
kcm
CC BY-SA 4.0
2019-04-04T08:23:19.077Z
2019-04-04T08:23:19.077Z
zx8754
<pre><code>colnames(countdata) = gsub("Aligned.sortedByCoord.out.bam_rem.bam", "", colnames(countdata))
colnames(countdata)
dput(colnames(countdata))
    c("BAF60A_KD_1_S13", "BAF60A_KD_2_S14", "BAF60A_RA_1_S17", "BAF60A_RA_2_S18", 
    "BAF60A_VD3_1_S15", "BAF60A_VD3_2_S16", "BAF60C_KD_1_UI_S1", 
    "BAF60C_KD_2_UI_S2", "BAF60C_KD_RA_1_S11", "BAF60C_KD_RA_2_S12", 
    "BAF60C_KD_VD3_1_S9", "BAF60C_KD_VD3_2_S10", "VC_1_UI_S3", "VC_2_UI_S4", 
    "VC_RA1_S5", "VC_RA_2_S6", "VC_VD3_1_S7", "VC_VD3_2_S8")
</code></pre>

<p>After removing the above pattern I want to remove this part from each of the colnames <code>_S[anynumber]</code> so that I can end up like this as an example  <code>BAF60A_KD_1</code> but not sure how to make that pattern and match the same and remove or replace it with empty space. I tried as below which ends up like all the digits are removed.</p>

<pre><code>colnames(countdata) = gsub("[[:digit:]_S]", "", colnames(countdata))

colnames(countdata)

c("BAFAKD", "BAFAKD", "BAFARA", "BAFARA", "BAFAVD", "BAFAVD", 
"BAFCKDUI", "BAFCKDUI", "BAFCKDRA", "BAFCKDRA", "BAFCKDVD", "BAFCKDVD", 
"VCUI", "VCUI", "VCRA", "VCRA", "VCVD", "VCVD")    
</code></pre>

<p>Any suggestion would be helpful</p>

CC BY-SA 4.0
2019-04-03T12:51:23.193Z
I'm voting to close this question as off-topic because the question is purely about regex, not requiring any biological knowledge. You can remove them with `names <- gsub("_S.*", "", colnames(countdata))` followed by removing any number or underscore `gsub("[:digit:]|_", "", names)`
llrs
CC BY-SA 4.0
2019-04-03T14:22:50.077Z
IIRC `gsub` has a `perl=TRUE` option for working with advanced regexes. Unfortunately, the `[[:digit:]]` style regexes are not supported by the base regex engines in most utilities/distros.
Ram RS
CC BY-SA 4.0
2019-04-03T16:08:01.577Z
@RamRS what do you mean? `[:digit:]` is a POSIX character class and should work on the vast majority of regex flavors. This isn't a feature of advanced regexes, but a feature of the most basic of them all: POSIX basic regular expressions. Where have you seen it not work? For example, it works in BRE (`printf "foo\n123\n" | grep -P '[[:digit:]]'`), ERE (`printf "foo\n123\n" | grep -E '[[:digit:]]'`), PCRE (`printf "foo\n123\n" | grep -P '[[:digit:]]'`).
terdon
CC BY-SA 4.0
2019-04-03T16:10:41.440Z
@llrs I don't think this is off topic. Bioinformatics involves a lot of basic programming knowledge, and I would say that includes regular expressions. You can do a lot of very productive work in bioinformatics without really knowing the first thing about biology. I would say this comes under the same category as general text parsing questions which [we've already decided](https://bioinformatics.meta.stackexchange.com/q/88/298) should be on topic.
terdon
CC BY-SA 4.0
2019-04-03T17:51:12.220Z
@terdon I use homebrew-powered GNU-sed on my MacBook Pro, and it won't `s/[:digit:]//` without the `-r` flag. That's the reason I added my disclaimer.
Ram RS
CC BY-SA 4.0
2019-04-03T17:59:08.230Z
@RamRS That should be `s/[[:digit:]]//`, which works on GNU sed without the `-r`. The `[:digit:]` is the special symbol, you need to put it in a character class (`[ ]`, hence `[[:digit:]]`) for it to work as a character class. `s/[:digit:]//` shouldn't (and doesn't, on my GNU sed running on Linux) work even with the `-r` (which just enables ERE). Even busybox sed (which is as basic as it gets) understands `[[:digit:]]` without `-r`.
terdon
CC BY-SA 4.0
2019-04-03T19:45:53.397Z
Apologies, I meant `'s/[[:digit:]]//'`. Weird that busybox sed works fine but my sed doesn't. I'll check again.
Ram RS
CC BY-SA 4.0
2019-04-03T19:49:10.697Z
Weird. Seems to work on all my computers now. I distinctly recall avoiding POSIX character classes because they worked inconsistently between my computers.
Ram RS
CC BY-SA 4.0
2019-04-04T06:46:40.310Z
and it really helped me .even though its really a non-biological or bio info question but these minute things helps me a lot
kcm
<r>
Pattern matching and removing from the name
# Pattern matching and removing from the name

(Asked by: username_0 on 2019-04-03)

colnames(countdata) = gsub("Aligned.sortedByCoord.out.bam_rem.bam", "", colnames(countdata))
colnames(countdata)
dput(colnames(countdata))
    c("BAF60A_KD_1_S13", "BAF60A_KD_2_S14", "BAF60A_RA_1_S17", "BAF60A_RA_2_S18", 
    "BAF60A_VD3_1_S15", "BAF60A_VD3_2_S16", "BAF60C_KD_1_UI_S1", 
    "BAF60C_KD_2_UI_S2", "BAF60C_KD_RA_1_S11", "BAF60C_KD_RA_2_S12", 
    "BAF60C_KD_VD3_1_S9", "BAF60C_KD_VD3_2_S10", "VC_1_UI_S3", "VC_2_UI_S4", 
    "VC_RA1_S5", "VC_RA_2_S6", "VC_VD3_1_S7", "VC_VD3_2_S8")

After removing the above pattern I want to remove this part from each of the colnames _S[anynumber] so that I can end up like this as an example  BAF60A_KD_1 but not sure how to make that pattern and match the same and remove or replace it with empty space. I tried as below which ends up like all the digits are removed.
colnames(countdata) = gsub("[[:digit:]_S]", "", colnames(countdata))

colnames(countdata)

c("BAFAKD", "BAFAKD", "BAFARA", "BAFARA", "BAFAVD", "BAFAVD", 
"BAFCKDUI", "BAFCKDUI", "BAFCKDRA", "BAFCKDRA", "BAFCKDVD", "BAFCKDVD", 
"VCUI", "VCUI", "VCRA", "VCRA", "VCVD", "VCVD")    

Any suggestion would be helpful

**Tags:** r
> **Comments:**
> **username_1**: I'm voting to close this question as off-topic because the question is purely about regex, not requiring any biological knowledge. You can remove them with `names <- gsub("_S.*", "", colnames(countdata))` followed by removing any number or underscore `gsub("[:digit:]|_", "", names)`  
> **username_2**: IIRC `gsub` has a `perl=TRUE` option for working with advanced regexes. Unfortunately, the `[[:digit:]]` style regexes are not supported by the base regex engines in most utilities/distros.  
> **username_3**: @RamRS what do you mean? `[:digit:]` is a POSIX character class and should work on the vast majority of regex flavors. This isn't a feature of advanced regexes, but a feature of the most basic of them all: POSIX basic regular expressions. Where have you seen it not work? For example, it works in BRE (`printf "foo\n123\n" | grep -P '[[:digit:]]'`), ERE (`printf "foo\n123\n" | grep -E '[[:digit:]]'`), PCRE (`printf "foo\n123\n" | grep -P '[[:digit:]]'`).  
> **username_3**: @llrs I don't think this is off topic. Bioinformatics involves a lot of basic programming knowledge, and I would say that includes regular expressions. You can do a lot of very productive work in bioinformatics without really knowing the first thing about biology. I would say this comes under the same category as general text parsing questions which [we've already decided](https://bioinformatics.meta.stackexchange.com/q/88/298) should be on topic.  
> **username_2**: @terdon I use homebrew-powered GNU-sed on my MacBook Pro, and it won't `s/[:digit:]//` without the `-r` flag. That's the reason I added my disclaimer.  
> **username_3**: @RamRS That should be `s/[[:digit:]]//`, which works on GNU sed without the `-r`. The `[:digit:]` is the special symbol, you need to put it in a character class (`[ ]`, hence `[[:digit:]]`) for it to work as a character class. `s/[:digit:]//` shouldn't (and doesn't, on my GNU sed running on Linux) work even with the `-r` (which just enables ERE). Even busybox sed (which is as basic as it gets) understands `[[:digit:]]` without `-r`.  
> **username_2**: Apologies, I meant `'s/[[:digit:]]//'`. Weird that busybox sed works fine but my sed doesn't. I'll check again.  
> **username_2**: Weird. Seems to work on all my computers now. I distinctly recall avoiding POSIX character classes because they worked inconsistently between my computers.  
> **username_0**: and it really helped me .even though its really a non-biological or bio info question but these minute things helps me a lot  

---

## 1 answers

### Answer by username_4 on 2019-04-04. Score: 3 | Accepted answer

Without regex, split on "_", drop last item, then paste it back again:
sapply(strsplit(x, "_"), function(i) paste(head(i, -1), collapse = "_"))

Or with regex (stealing the code from tools::file_path_sans_ext function which is used to drop file extensions):
sub("([^_]+)\\_[[:alnum:]]+$", "\\1", x)

# [1] "BAF60A_KD_1"     "BAF60A_KD_2"     "BAF60A_RA_1"     "BAF60A_RA_2"     "BAF60A_VD3_1"    "BAF60A_VD3_2"   
# [7] "BAF60C_KD_1_UI"  "BAF60C_KD_2_UI"  "BAF60C_KD_RA_1"  "BAF60C_KD_RA_2"  "BAF60C_KD_VD3_1" "BAF60C_KD_VD3_2"
# [13] "VC_1_UI"         "VC_2_UI"         "VC_RA1"          "VC_RA_2"         "VC_VD3_1"        "VC_VD3_2"

> **Comments:**
> **username_0**: multiple ways of doing wow  
> **username_4**: @krushnachChandra minor note, you will get a quicker and better answers if you post regex/R posts at StackOverflow.  
> **username_0**: well actually i got banned from posting it on the R forum ,i guess i asked stupid questions.it says from this account no more questions.But im glad for multiple answers coming out  
> **username_4**: What do you mean "banned"? This is your profile, right? https://stackoverflow.com/users/6342356/krushnach-chandra  
> **username_0**: yes i do ask question on this forum and other forum but not in the R section may be bad questions  
> **username_0**: This is what i see when i try to ask question on that forum "You have reached your question limit Sorry, we are no longer accepting questions from this account. See the Help Center to learn more."  
> **username_4**: @krushnachChandra Looks like you have a system ban "for asking poor questions", it is temporary, not sure for how long. Relevant meta post regarding ban: https://meta.stackexchange.com/q/86997/228487  
> **username_0**: okay no issues..i as quite naive to ask certain question ..  

---

