可能的重复: 在java中打印正则表达式匹配
我在java中使用Matcher类来将字符串与特定的正则表达式相匹配,我使用Pattern类将其转换为Pattern。我知道我的正则表达式有效,因为当我执行 Matcher.find() 时,我得到了我应该得到的真实值。但我想打印出产生这些真实值的字符串(意味着打印出与我的正则表达式匹配的字符串),并且我在匹配器类中没有看到实现该目的的方法。如果有人以前遇到过这样的问题,请告诉我。我很抱歉,因为这个问题相当初级,但我对正则表达式相当陌生,因此仍在正则表达式世界中寻找方法。
我有一个简单的递归函数将布尔值列表转换为字符串:
def boolsToString(lst: List[Boolean]): String = lst match {
case Nil => ""
case x::xs => x match {
case false => "0" + boolsToString(xs)
case true => "1" + boolsToString(xs)
}
}
Run Code Online (Sandbox Code Playgroud)
这可行,但我不喜欢重复 boolsToString。我只想进行一次字符串连接(在大小写之后):
def boolsToString2(lst: List[Boolean]): String = lst match {
case Nil => ""
case x::xs => x match {
case false => "0"
case true => "1"
} + boolsToString2(xs)
}
Run Code Online (Sandbox Code Playgroud)
但这被 Scala 编译器拒绝:“';' 符合预期,但找到了标识符。”
在这种情况之后,还有另一种方法可以只进行一次字符串连接吗?
从https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_replace2获取
现在我想更改字符串中的+91。来自https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_replace2
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "blue" with "red" in the paragraph below:</p>
<p id="demo">Mr Blue has a blue house and a blue car.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/blue/g, "red");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
让我们考虑以下字符串。
字符串:- 嗨,我的号码是 +919090909090 和 +9190820209282 等等。
我想要的结果如下:嗨,我的号码是 +91 - 9090909090 和 +91 - 90820209282 等等。
但是当我使用正则表达式模式时,似乎在使用时出现错误
str.replace(/blue/g, "red");
无效的正则表达式:/+91/:没有可重复的内容”
这段代码中哪一段是 Erlang 中的首选方式,为什么?
sumOfMultiples(Multiples, 1) ->
0;
sumOfMultiples(Multiples, N) ->
cal_multiples(Multiples, lists:seq(1, N-1), 0).
Run Code Online (Sandbox Code Playgroud)
或者
sumOfMultiples(Multiples, N) ->
case N of
1 -> 0;
cal_multiples(Multiples, lists:seq(1, N-1), 0)
end
Run Code Online (Sandbox Code Playgroud) 我有一列包含扫描条形码的字符串。我想找到特定的匹配项并在新列中返回字符串“match”和“noMatch”
条形码的正则表达式是
'[0-9]{5,8}\%[0-9]*\%'
例如13412432%10000%
我的查询是
SELECT
report."barcode" SIMILAR TO '[0-9]{5,8}\%[0-9]*\%',
(CASE report."barcode" WHEN (report."barcode" SIMILAR TO '[0-9]{5,8}\%[0-9]*\%') THEN 'match'
ELSE 'noMatch'
END) AS matchColumn
FROM report
Run Code Online (Sandbox Code Playgroud)
但是我总是收到这个错误
错误:运算符不存在:文本 = 布尔值第 3 行:(CASE 报告。“条形码”WHEN (报告。“条形码...
提示:没有运算符与给定名称和参数类型匹配。您可能需要添加显式类型转换。SQL 状态:42883 字符:106
我对 SQL 相当陌生,所以当文档说它在 WHEN 之后需要一个 true/false 语句时,我认为我可以使用 SIMIAR TO 模式匹配,因为它返回布尔值。
我发现使用match比if. 如果我有一个boolean值,我可以将它与 一起使用match吗?
我通常这样做
if(!authorised) {...} else {..}
Run Code Online (Sandbox Code Playgroud)
但我无法做到
authorised match {
case ??? //what here??
}
Run Code Online (Sandbox Code Playgroud) 我想使用模式匹配来替换多个if语句,如下所示的方法Select<T>()。我想使用switch()上的声明typeof(T)。
static class Program
{
static void Main(string[] args)
{
var doc = new Document();
IWire select = doc.Select<IWire>();
}
public static T Select<T>(this Document document) where T : class, IGeneric
{
var t = typeof(T);
if (t.IsAssignableTo(typeof(IWire)))
{
return document.SelectEntity(EntityType.Wire) as T;
}
if (t.IsAssignableTo(typeof(ISolid)))
{
return document.SelectEntity(EntityType.Solid) as T;
}
if (t.IsAssignableTo(typeof(ISurface)))
{
return document.SelectEntity(EntityType.Surface) as T;
}
// imagine a lot of if statements here
return null;
}
} …Run Code Online (Sandbox Code Playgroud) 我在网上看到了以下正则表达式,并想将其实现到我的 Java 应用程序中(使用 java.util.regex)。
\n\n(?<=(<Anhang>))(\\w|\\d|\\n|[().,\\-:;@#$%^&*\\[\\]"\'+\xe2\x80\x93/\\/\xc2\xae\xc2\xb0\xe2\x81\xb0!?{}|`~]| )+?(?=(<\\/Anhang>))\nRun Code Online (Sandbox Code Playgroud)\n\n这应该与 中包含的任何内容匹配\'<Anhang>\'。
它在 JavaScript 引擎中工作正常,但我无法让它在 Java 中工作。
\n\n在这里,我使用 JavaScript 引擎在regex101上针对此文本进行了测试:
\n\nBLALBLA BLA BLA <Anhang> \ngonegone gone gone ,os .psd\n</Anhang> ajdajadw\nRun Code Online (Sandbox Code Playgroud)\n\n产生以下结果:
\n\n\n\n所以我继续尝试在“ Java正则表达式测试器”中使用它,但它要么与文本不匹配,要么存在语法错误。\n我知道我必须转义某些字符,但我只是没有让它工作,这是我尝试过的:
\n\n(?<=(<Anhang>))(\\\\w|\\\\d|\\\\n|[().,\\-:;@#$%^&*\\[\\\\]\\"\'+\xe2\x80\x93/"/\xc2\xae\xc2\xb0\xe2\x81\xb0!?{}|`~]| )+?(?=(<\\"Anhang>))\n\n(?<=(<Anhang>))(\\\\w|\\\\d|\\\\n|[().,\\-:;@#$%^&*\\[\\\\]\\"\'+\xe2\x80\x93/"/\xc2\xae\xc2\xb0\xe2\x81\xb0!?\\{\\}|`~]| )+?(?=(<\\"Anhang>))\n\n(?<=(<Anhang>))(\\\\w|\\\\d|\\\\n|[().,\\\\\\\\-:;@#$%^&*\\[\\\\]\\"\'+\xe2\x80\x93/"/\xc2\xae\xc2\xb0\xe2\x81\xb0!?\\{\\}|`~]| )+?(?=(<\\"Anhang>))\nRun Code Online (Sandbox Code Playgroud)\n 使用{:typed_struct, "~> 0.2.1"}库我有以下结构:
defmodule RequestParams do
use TypedStruct
typedstruct do
field :code, String.t()
field :name, String.t()
end
end
Run Code Online (Sandbox Code Playgroud)
我正在尝试将函数参数模式匹配到结构:
def do_handle(params %RequestParams{}, _context) do
# instead of
# def do_handle(%{ "code" => code, "name" => name}, _context) do
Run Code Online (Sandbox Code Playgroud)
但我得到例外:
Run Code Online (Sandbox Code Playgroud)cannot find or invoke local params/2 inside match. Only macros can be invoked in a match and they must be defined before their invocation.
怎么了?是否有可能将函数参数与结构相匹配?
今天只需要为一个非常接近此的项目编写一些代码:
private static string CreateColorFromPercentage(double percentage, bool isPrimaryGroup)
{
if (isPrimaryGroup)
{
if (percentage >= 97.0)
{
return "#000000"; // black
}
else if (percentage > 80.0)
{
return "#FF0000"; // red
}
else
{
return "#FFA500"; // orange
}
}
else
{
if (percentage > 97.5)
{
return "#000000"; // black
}
else if (percentage > 80.0)
{
return "#FFA500"; // orange
}
else
{
return "#008000"; // green
}
}
}
Run Code Online (Sandbox Code Playgroud)
在测试范围时,是否有更多的 C# 9 / .NET 5 惯用方式来表达 …
pattern-matching ×10
regex ×4
c# ×2
java ×2
scala ×2
.net-5 ×1
c#-7.0 ×1
elixir ×1
erlang ×1
generics ×1
html ×1
idioms ×1
if-statement ×1
javascript ×1
postgresql ×1
readability ×1
sql ×1
string ×1
struct ×1