regex

How to shave off last character using sed?

六眼飞鱼酱① 提交于 2021-02-06 14:49:23
问题 That is, going from ABCD -> ABC 回答1: You can try: sed s'/.$//' The regex used is .$ . is a regex meta char to match anything (except newline) $ is the end of line anchor. By using the $ we force the . to match the last char This will remove the last char, be it anything: $ echo ABCD | sed s'/.$//' ABC $ echo ABCD1 | sed s'/.$//' ABCD But if you want to remove the last char, only if its an alphabet, you can do: $ echo ABCD | sed s'/[a-zA-Z]$//' ABC $ echo ABCD1 | sed s'/[a-zA-Z]$//' ABCD1 回答2:

How to shave off last character using sed?

ぃ、小莉子 提交于 2021-02-06 14:49:11
问题 That is, going from ABCD -> ABC 回答1: You can try: sed s'/.$//' The regex used is .$ . is a regex meta char to match anything (except newline) $ is the end of line anchor. By using the $ we force the . to match the last char This will remove the last char, be it anything: $ echo ABCD | sed s'/.$//' ABC $ echo ABCD1 | sed s'/.$//' ABCD But if you want to remove the last char, only if its an alphabet, you can do: $ echo ABCD | sed s'/[a-zA-Z]$//' ABC $ echo ABCD1 | sed s'/[a-zA-Z]$//' ABCD1 回答2:

Error compiling a verbose Java regex with character class and word boundary

倖福魔咒の 提交于 2021-02-06 14:28:14
问题 Why does this pattern fail to compile : Pattern.compile("(?x)[ ]\\b"); Error ERROR java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 8 (?x)[ ]\b ^ at java_util_regex_Pattern$compile.call (Unknown Source) While the following equivalent ones work? Pattern.compile("(?x)\\ \\b"); Pattern.compile("[ ]\\b"); Pattern.compile(" \\b"); Is this a bug in the Java regex compiler, or am I missing something? I like to use [ ] in verbose regex instead of backslash

Error compiling a verbose Java regex with character class and word boundary

邮差的信 提交于 2021-02-06 14:26:45
问题 Why does this pattern fail to compile : Pattern.compile("(?x)[ ]\\b"); Error ERROR java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 8 (?x)[ ]\b ^ at java_util_regex_Pattern$compile.call (Unknown Source) While the following equivalent ones work? Pattern.compile("(?x)\\ \\b"); Pattern.compile("[ ]\\b"); Pattern.compile(" \\b"); Is this a bug in the Java regex compiler, or am I missing something? I like to use [ ] in verbose regex instead of backslash

Error compiling a verbose Java regex with character class and word boundary

…衆ロ難τιáo~ 提交于 2021-02-06 14:21:28
问题 Why does this pattern fail to compile : Pattern.compile("(?x)[ ]\\b"); Error ERROR java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 8 (?x)[ ]\b ^ at java_util_regex_Pattern$compile.call (Unknown Source) While the following equivalent ones work? Pattern.compile("(?x)\\ \\b"); Pattern.compile("[ ]\\b"); Pattern.compile(" \\b"); Is this a bug in the Java regex compiler, or am I missing something? I like to use [ ] in verbose regex instead of backslash

Extracting phone number issue in R

亡梦爱人 提交于 2021-02-06 13:57:37
问题 Having numbers like this: ll <- readLines(textConnection("(412) 573-7777 opt 1 563.785.1655 x1797 (567) 523-1534 x7753 (567) 483-2119 x 477 (451) 897-MALL (342) 668-6255 ext 7 (317) 737-3377 Opt 4 (239) 572-8878 x 3 233.785.1655 x1776 (138) 761-6877 x 4 (411) 446-6626 x 14 (412) 337-3332x19 412.393.3177 x24 327.961.1757 ext.4")) What is the regex I should write to get: xxx-xxx-xxxx I tried this one: gsub('[(]([0-9]{3})[)] ([0-9]{3})[-]([0-9]{4}).*','\\1-\\2-\\3',ll) It doesn't cover all the

Extracting phone number issue in R

喜你入骨 提交于 2021-02-06 13:57:31
问题 Having numbers like this: ll <- readLines(textConnection("(412) 573-7777 opt 1 563.785.1655 x1797 (567) 523-1534 x7753 (567) 483-2119 x 477 (451) 897-MALL (342) 668-6255 ext 7 (317) 737-3377 Opt 4 (239) 572-8878 x 3 233.785.1655 x1776 (138) 761-6877 x 4 (411) 446-6626 x 14 (412) 337-3332x19 412.393.3177 x24 327.961.1757 ext.4")) What is the regex I should write to get: xxx-xxx-xxxx I tried this one: gsub('[(]([0-9]{3})[)] ([0-9]{3})[-]([0-9]{4}).*','\\1-\\2-\\3',ll) It doesn't cover all the

python regex fails to identify markdown links

北城以北 提交于 2021-02-06 13:55:00
问题 I am trying to write a regex in python to find urls in a Markdown text string. Once a url is found, I want to check if this is wrapped by a markdown link: text I am having problem with the latter. I am using a regex - link_exp - to search, but the results are not what I expected, and cannot get my head around it. This is probably something simple that I am not seeing. here goes the code and explanation of the link_exp regex import re text = ''' [Vocoder](http://en.wikipedia.org/wiki/Vocoder )

python regex fails to identify markdown links

大城市里の小女人 提交于 2021-02-06 13:46:26
问题 I am trying to write a regex in python to find urls in a Markdown text string. Once a url is found, I want to check if this is wrapped by a markdown link: text I am having problem with the latter. I am using a regex - link_exp - to search, but the results are not what I expected, and cannot get my head around it. This is probably something simple that I am not seeing. here goes the code and explanation of the link_exp regex import re text = ''' [Vocoder](http://en.wikipedia.org/wiki/Vocoder )

Add quotes around each word

痞子三分冷 提交于 2021-02-06 12:42:49
问题 I want to transform in Java: dd fdas dd fdas fads f das fdasf + - || dasf into: "dd" "fdas" "dd" "fdas" "fads" "f" "das" "fdasf" + - || "dasf" basically I want to add quotes around words. \w* -> "\w*\" 回答1: replaceAll can do this: String result = input.replaceAll("(\w+)", "\"$1\""); 回答2: It's fairly simple. Use preg_replace and sorround all text captures with quotes. preg_replace("/([a-z0-9]+)/i", '"$1"', $string); You will have to find the replacement of preg_replace for java :) 来源: https:/