我正在编写一个脚本来帮助我将一组文本文件转换为markdown.这个脚本所做的一件事就是将斜体和标题格式应用于图形标题,这些标题是以一些空格和单词"图"开头的行.这是我的代码:
text = Regex.Replace(text, "^ +(Figure.*)$", "##### _$1_", RegexOptions.Multiline);
Run Code Online (Sandbox Code Playgroud)
如果我用它来转换这个文本:
A Foobar is cool stuff, as we can see in Figure 1.1:
Figure 1.1 This is a Foobar
More text here.
Run Code Online (Sandbox Code Playgroud)
...然后我明白了:
A Foobar is cool stuff, as we can see in Figure 1.1:
##### _Figure 1.1 This is a Foobar _
More text here.
Run Code Online (Sandbox Code Playgroud)
...除了一个小细节外,我想要的是:在LinqPad输出窗口的最后一个下划线字符之前添加了一个空格.我不知道它来自何处,因为它不存在于原始文本中(在"Foobar"之后有一个CRLF序列).我的正则表达式或我如何使用它有什么问题?
编辑:完整的可执行程序演示问题:
using System;
using System.Text.RegularExpressions;
class Test
{
static void Main()
{
string text =
@"A Foobar is cool stuff, as we can …Run Code Online (Sandbox Code Playgroud) 无法在
以单引号或双引号?结尾的行中实现正则表达式;。
例如:
?abcdef;
'?abcdef;'
"?abcdef;"
Run Code Online (Sandbox Code Playgroud)
我做了很多尝试,就像这样,"^\\?([^;]+)\\;$"但是没有用。
测试代码段:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
public static void main(final String[] args) {
final String text = "This is param-start ?abcdef; param-end";
final String patternString = "(['\"]?)\\?.*;\\1";
final Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(text);
System.out.println("lookingAt = " + matcher.lookingAt());
System.out.println("matches = " + matcher.matches());
}
}
Run Code Online (Sandbox Code Playgroud) 我尝试读取街道地址并用正则表达式分割街道和号码。
该地址可以如下所示:
Ebory Mebory Street 1
Eborymebory Street 23/2
Filler Filler Filler Street 1-32
Run Code Online (Sandbox Code Playgroud)
我尝试拆分字符串使用split(),然后再次将街道名称放在一起(如果在拆分字符串中没有检测到数字)。
这仅适用于第一种情况。我认为如果我使用正则表达式会更好。问题是我对正则表达式的理解非常有限。
adress = "Ebory mebory Street 23".split()
street = ""
number = ""
for item in adress:
if(not(item.isdigit())):
street = street + item + " "
else:
number = item
Run Code Online (Sandbox Code Playgroud)
我得到的结果是正确的,但仅限于这种情况。如果地址是Eborymebory Street 23/24,则由于斜杠而无法工作。
那么,问题是如何获取街道地址以及字符串末尾的数字,并记住这些数字可能是可选的?
我有一个看起来像这样的字符串......
mystring = '{#var#}status has been updated{#var#}with remark{#var#}connect with{#var#}at{#var#}transactions may be{#var#}till{#var#}'
Run Code Online (Sandbox Code Playgroud)
我只需要给它编号,让它看起来像这样......
{#var1#}status has been updated{#var2#}with remark{#var3#}connect with{#var4#}at{#var5#}transactions may be{#var6#}till{#var7#}
Run Code Online (Sandbox Code Playgroud)
这将用数字 1 替换字符串
mystring.replace("{#var#}", "{#var1#}")
Run Code Online (Sandbox Code Playgroud)
不知道如何枚举数字 2,3,4 到 7
我很好奇 Perl 正则表达式反向引用和其他人的(C++、grep、emacs,实际上我见过的所有其他用法)之间的语法差异的历史原因。
Perl\g1用于组反向引用。其他人都使用看起来更简洁的语法,只是\1.
我正在尝试删除文本,直到包含一个或多个逗号的字符串中的第一个逗号为止。出于某种原因,我发现这总是删除所有字符串的最后一个逗号之前的所有内容。
字符串看起来像:
OCR - (some text), Variant - (some text), Bad Subtype - (some text)
Run Code Online (Sandbox Code Playgroud)
我的正则表达式正在返回:
Bad Subtype - (some text)
Run Code Online (Sandbox Code Playgroud)
当所需的输出是:
Variant - (some text), Bad Subtype - (some text)
Run Code Online (Sandbox Code Playgroud)
Variant 不能保证排在第二位。
#select all strings beginning with OCR in the column Tags
clean<- subset(all, grepl("^OCR", all$Tags)
#trim the OCR text up to the first comma, and store in a new column called Tag
clean$Tag<- gsub(".*,", "", clean$Tag)
Run Code Online (Sandbox Code Playgroud)
或者
clean$Tag <- gsub(".*\\,", "", clean$Tag)
Run Code Online (Sandbox Code Playgroud)
或者
clean$Tag<- sub(".*,", "", clean$Tag)
Run Code Online (Sandbox Code Playgroud)
等等..
我有这个向量
names <- c("wazzzap12waaazzzaaaaapffffm12323", "hell223231", "musssaaaa225")
Run Code Online (Sandbox Code Playgroud)
所以我想删除向量元素中至少包含三个“z”字母的数字的单词。
我想用正则表达式删除重复的单词
let url = "/courses?category=programming&filter=free&filter=free"
const az = url.replace(/(\b\S.+\b)(?=.*\1)/g, "").trim();
console.log(az) // /courses?category=programmingfree&filter=free
Run Code Online (Sandbox Code Playgroud)
我想要得到/courses?category=programming&filter=free
为了显示我运行的回归结果,我有一个包含估计值和相应置信区间的小标题:
\nlibrary(tidyverse)\nlibrary(magrittr\n\nmydata <- structure(list(term = structure(c(1L, 3L, 4L), .Label = c("Intercept", \n"Follow-up time (years)", "Age (years)", "Sex (male)", "Never smoker (reference)", \n"Current smoker", "Former smoker", "Obesity (=30 kg/m\xc2\xb2)", "BMI (kg/m\xc2\xb2)", \n"Diabetes", "Glucose (mmol/L)", "Glucose lowering medication use", \n"Hypertension", "Systolic blood pressure (mmHg)", "Diastolic blood pressure (mmHg)", \n"Antihypertensive medication use", "Hypercholesterolemia", "LDL cholesterol (mmol/L)", \n"Lipid lowering medication use", "Chronic kidney disease (mL/min/1.73m\xc2\xb2)", \n"=90 (reference)", "60-89", "=60"), class = c("ordered", "factor"\n)), estimate = c(518.38, 0.98, 1.07), conf_low = c(178.74, 0.93, \n0.96), …Run Code Online (Sandbox Code Playgroud) 我有一个 .bed (.tsv) 文件,如下所示:
chr1 0 100000
chr1 100000 200000
chr1 200000 300000
chr1 300000 425234
Run Code Online (Sandbox Code Playgroud)
我想使用 sed 或 awk 仅对第 3 列中以“000”结尾的值执行操作 -1,以便输出如下所示:
chr1 0 99999
chr1 100000 199999
chr1 200000 299999
chr1 300000 425234
Run Code Online (Sandbox Code Playgroud)
令人尴尬的是,我想出的最好的办法是:
awk {sub(/000$/,"999",$3); print $1,$2,$3}' oldfile > newfile
Run Code Online (Sandbox Code Playgroud)
它只是用最后 3 位数字代替 999,而不是实际相减。任何帮助总是值得赞赏!