我正试图编写一个正则表达式来匹配'/'之前的字符串的一部分,但也忽略匹配中的任何前导或尾随空白。
到目前为止,我拥有^[^\/]*
,它与'/'之前的所有内容匹配,但是我不知道如何忽略空白。
123 / some text 123
应该屈服
123
和
a test / some text 123
应该屈服
a test
答案 0 :(得分:3)
有点棘手。您首先要从一个非空白字符开始匹配,然后继续缓慢但肯定地直到直到紧随其后的位置,然后是可选数量的空格和斜杠:
\S.*?(?= *\/)
如果斜杠可能是输入字符串中的第一个非空白字符,则将\S
替换为[^\s\/]
:
[^\s\/].*?(?= *\/)
答案 1 :(得分:2)
此表达式可能是您想要探索的:
^(.*?)(\s+\/.*)$
在这里,我们有两个捕获组,第一个捕获组收集您想要的输出,第二个捕获组是您不想要的模式,以开始和结束字符为界,只是为了安全起见,如果需要,可以将其删除:
(.*?)(\s+\/.*)
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(.*?)(\s+\/.*)$"
test_str = ("123 / some text 123\n"
"anything else / some text 123")
subst = "\\1"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
const regex = /^(.*?)(\s+\/.*)$/gm;
const str = `123 / some text 123
anything else / some text 123`;
const subst = `\n$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
如果这不是您想要的表达式,则可以在regex101.com中修改/更改表达式。
您还可以在jex.im中可视化您的表达式:
对于所需输出之前的空格,我们可以简单地使用negative lookbehind添加一个捕获组:
^(\s+)?(.*?)(\s+\/.*)$
const regex = /^(\s+)?(.*?)(\s+\/.*)$/gm;
const str = ` 123 / some text 123
anything else / some text 123
123 / some text 123
anything else / some text 123`;
const subst = `$2`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
答案 2 :(得分:2)
这是一个可能的解决方案
正则表达式
isThisDeviceTheOneIWant :: Int -> VendorId -> ProductId -> IO Bool
isThisDeviceTheOneIWant n a b = ((a, b) ==) <$> getOneDesc n
示例
(?<!\/)\S.*\S(?=\s*\/)
简短说明
# import regex # or re
string = ' 123 / some text 123'
test = regex.search(r'(?<!\/)\S.*\S(?=\s*\/)', string)
print(test.group(0))
# prints '123'
string = 'a test / some text 123'
test = regex.search(r'(?<!\/)\S.*\S(?=\s*\/)', string)
print(test.group(0))
# prints 'a test'
说,在可能的匹配之前,没有(?<!\/)
符号。/
随便匹配任何内容(\S.*\S
),同时确保它不以空格开头或结尾(.*
)\S
表示可能的匹配项后必须紧跟(?=\s*\/)
符号或空格+ /
。答案 3 :(得分:1)
您可以不使用正则表达式
library(ggplot2)
library(tidyverse)
library(ggthemes)
SOtestplot <- test %>%
filter(Species == "MEK", binned_alt <= 8000) %>%
ggplot(aes(y = binned_alt/1000, x = `50%`, col = Meteorological_Season_Factor, linetype = Treatment)) +
geom_path(size = 1.2) +
theme_tufte(base_size = 22) +
geom_errorbarh(aes(xmin =`25%`, xmax = `75%`), height = 0) +
theme(axis.title.x = element_text(vjust=-0.5),
axis.title.y = element_text(vjust=1.5),
panel.grid.major = element_line(colour = "grey80"),
axis.line = element_line(size = 0.5, colour = "black")) +
scale_color_manual(name = "Treatment & Season",
values = c("cornflowerblue", "goldenrod3", "cornflowerblue", "goldenrod3"),
labels = c("Summer Clean Marine",
"Winter Clean Marine",
"Summer All Data",
"Winter All Data")) +
scale_linetype_manual(name = "Treatment & Season",
values = c( "solid", "dashed", "solid", "dashed"),
labels = c("Summer Clean Marine",
"Winter Clean Marine",
"Summer All Data",
"Winter All Data")) +
xlab("MEK (ppt)") +
ylab("Altitude (km)")