正则表达式大写首字母每一个字,之后还像一个破折号特殊字符
我用这个#(\s|^)([a-z0-9-_]+)#i
为大写每个第一个字母每一个字,我想这也是大写字母,如果它像一个破折号特殊标记后( - )正则表达式大写首字母每一个字,之后还像一个破折号特殊字符
现在它显示:
This Is A Test For-stackoverflow
,我想这一点:
This Is A Test For-Stackoverflow
任何建议/样品给我吗?
却不是因亲,所以尽量保持简单,我听不懂。
尝试#([\s-]|^)([a-z0-9-_]+)#i
- 在(\s|^)
空白字符(\s
)或匹配该行的开始(^
)。当您将\s
更改为[\s-]
时,它会匹配任何空格字符或破折号。
谢谢!像js中的魅力 – Simmer 2011-06-06 11:55:48
其实不需要匹配满弦只是匹配这样的第一个非大写字母:
'~\b([a-z])~'
一样工作,我已经添加'g' like'/ \ b([az])/ g'来大写每个单词 – 2014-12-06 07:53:08
我喜欢你可爱的答案@StalinGino必须说这是我唯一能够了解。 – Danish 2016-02-08 11:38:41
这将使
REAC德Boeremeakers
从
reac de boeremeakers
(?<=\A|[ .])(?<up>[a-z])(?=[a-z. ])
使用
Dim matches As MatchCollection = Regex.Matches(inputText, "(?<=\A|[ .])(?<up>[a-z])(?=[a-z. ])")
Dim outputText As New StringBuilder
If matches(0).Index > 0 Then outputText.Append(inputText.Substring(0, matches(0).Index))
index = matches(0).Index + matches(0).Length
For Each Match As Match In matches
Try
outputText.Append(UCase(Match.Value))
outputText.Append(inputText.Substring(Match.Index + 1, Match.NextMatch.Index - Match.Index - 1))
Catch ex As Exception
outputText.Append(inputText.Substring(Match.Index + 1, inputText.Length - Match.Index - 1))
End Try
Next
+1 word边界,这里是一个可比的JavaScript解决方案。这也解释了所有格:
var re = /(\b[a-z](?!\s))/g;
var s = "fort collins, croton-on-hudson, harper's ferry, coeur d'alene, o'fallon";
s = s.replace(re, function(x){return x.toUpperCase();});
console.log(s); // "Fort Collins, Croton-On-Hudson, Harper's Ferry, Coeur D'Alene, O'Fallon"
toUpperCase正在大写整个单词。这里是解决方案: s.replace(re,function(x){return x.charAt(0).toUpperCase()+ x.slice(1);}); – Polopollo 2016-05-09 20:26:41
@Polopollo,在这种情况下,正则表达式只会返回一个字母,如果它匹配但全局。所以不需要额外的编码,它应该按原样工作。 – 2017-04-26 19:51:00
由于OP询问过单个角色不会被大写,因此这不起作用。只是对于像我这样的人来这个问题。 – 2017-04-26 19:51:56
您是否还需要大写非ASCII字母('à','ü'等)?你在用什么语言? – 2011-06-06 13:10:32
你问什么语言的正则表达式? – JohnK 2017-06-22 15:32:44