Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to remove a letter and dot combination

I am trying to use regular expressions in R to remove text - either an 'X' or an 'X.' - from the front of a number. I am new to regular expressions and having a hard time getting this to work. I have tried every combination of X and . with or without the escape character that I could think of, including:

  • str_replace("X.4.89294e-05", "X.",'') Result "4.89294e-05" but for fails for str_replace("X4.89294e-05", "X.",'')Result ".89294e-05"
  • str_replace("X.4.89294e-05", "[X.]",'') Result ".4.89294e-05"
  • str_replace("X.4.89294e-05", "[X/.?]",'') Result ".4.89294e-05"
  • str_replace("X.4.89294e-05", "[X//.?]",'') Result ".4.89294e-05"
  • str_replace('X.4.89294e-0','X/.{0,1}','') Result "X.4.89294e-0"
  • str_replace('X.4.89294e-0','[X/.{0,1}]','') Result ".4.89294e-0"

Any help would be greatly appreciated.

like image 656
mrpargeter Avatar asked Apr 20 '18 03:04

mrpargeter


People also ask

How do I remove a specific character from a regular expression?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do you escape a dot in regex?

(dot) metacharacter, and can match any single character (letter, digit, whitespace, everything). You may notice that this actually overrides the matching of the period character, so in order to specifically match a period, you need to escape the dot by using a slash \.


2 Answers

The . must be escaped. In R, you do that by adding a \\ before the . operator.

Read on the need for \\ here: Escape with a double backslash

Like this:

txt = c("X.4.89294e-0", "X4.89294e-0")
str_replace(txt, "^X(\\.)?", "")

If you do not want to specifically match X or X. at the very beginning, remove ^ from the example above to match it anywhere in the word.

like image 79
Deepak Rajendran Avatar answered Oct 12 '22 23:10

Deepak Rajendran


You mean remove 'X' Or 'X.' From any digits?
Actually an single . in regex should be like this \., so try str_replace("X.4.89294e-05", "X\.?", "") instead.

like image 43
Alex.Fu Avatar answered Oct 13 '22 00:10

Alex.Fu