[英]Regex - Replace single character after a string in scala
I have a text similar to: 我的文字类似于:
"ciao cos? come stai??"
And I'd want to replace (in Scala using Regex) only one question mark after a sequence of characters (ie [a-zA-Z0-9]) with another character. 我想在一个字符序列(即[a-zA-Z0-9])之后用另一个字符替换(在使用Regex的Scala中)一个问号。 So in the previous example if we suppose that I want to replace "?" 因此,在前面的示例中,如果我们假设我要替换“?” with "_", the result should be: 带有“ _”的结果应为:
"ciao cos_ come stai_?"
Edit : Yes, I tried some solution found also on SO, like this in this link . 编辑 :是的,我尝试了一些也在SO上找到的解决方案,例如此链接中的 。 In Scala I tried: 在Scala中,我尝试过:
val text = "some? ??"
val regex = "/([a-zA-Z0-9])?/".r
val text11 =regex.replaceAllIn(text, "_")
But also: 但是也:
val text = "some? ??"
val regex = "/([a-zA-Z0-9])?([a-zA-Z0-9])/".r
val text11 =regex.replaceAllIn(text, "_")
And the original one posted in the previous link with another string in input but it doesn't work. 原始链接发布在上一个链接中,并在输入中添加了另一个字符串,但它不起作用。
Thanks 谢谢
I don't know about scala, but after some research I manage to build something for you. 我不了解scala,但经过研究后,我设法为您构建了一些东西。
Here the regex if you want to deal only with english characters 如果您只想处理英文字符,请在这里使用正则表达式
val str = "ciao cos? come stai??".replaceAll("""((?i)[A-Z]+)\?""", "$1_");
Explanation : 说明 :
(?i)
: Means case insensitive. (?i)
:表示不区分大小写。 [AZ]+
: One or more english letter [AZ]+
:一个或多个英文字母 ()
: Capture group ()
:捕获组 ((?i)[AZ]+)
: capture one or more english letter (no matter the case) ((?i)[AZ]+)
:捕获一个或多个英文字母(无论大小写) (\\?)
: Capture the literal character '?' (\\?)
:捕获文字字符“?” in the second group (it have to be escaped with a backslash because the question mark have a special meaning in the regex). 在第二组中(它必须用反斜杠转义,因为问号在正则表达式中具有特殊含义)。 ((?i)[AZ]+)\\?
: Capture as much letters as you can in the first capture group immediately followed by a question mark captured by the second capture group. :立即在第一个捕获组中捕获尽可能多的字母,然后在第二个捕获组中捕获一个问号。
$1
: Put the content of the first capture group $1
:放置第一个捕获组的内容
$1_
: Put the content of the first capture group followed by an underscore. $1_
:将第一个捕获组的内容放在下划线之后。 The question mark will disappear. 问号将消失。 To deal with any letters from any languages (by example the french letter "é" you can use this: 要处理来自任何语言的任何字母(例如,法语字母“é”,您可以使用以下代码):
val str = "j'aime le karaté?".replaceAll("""(\p{L}+)\?""", "$1_");
\\p{L}
: That stand for any unicode letter in any case. \\p{L}
:在任何情况下,代表任何unicode字母。 I used this site to test the regexes: 我使用此站点来测试正则表达式:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.