简体   繁体   English

正则表达式替换所有前导字符而不是a-Z

[英]Regex to replace all leading characters not a-Z

I need to replace all non-letter characters that appear at the start before any letter for example 我需要替换在任何字母之前出现在开头的所有非字母字符

$ %5hello w8r^ld becomes hello w8r^ld $%5hello w8r ^ ld成为你好w8r ^ ld

This regex I got now works greate for replacing none word characters but does not replace numbers 我现在得到的这个正则表达式可以用来替换任何单词字符,但不会替换数字

s.replaceFirst("^[\\W_]+", "")

You are using the wrong character class. 您正在使用错误的字符类。 Use 使用

s.replaceFirst("^[^a-zA-Z]+", "")

That is 那是

^          start at the beginning of the string
[^  ]+     one or more (greedy - keep going until you hit a letter
a-zA-Z     ascii characters between a-z or A-Z

Following comments from @anubhava, I changed the * to a + . 根据@anubhava的评论,我将*更改为+ If you have no match, there is nothing that needs replacing. 如果没有匹配,则无需更换。 It's actually cleaner. 它实际上更清洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 替换字符串az 0-9和旁边的所有字符, - Replace all characters in string beside a-z 0-9 and , Java Regex az,AZ,0-9和(。)(_)( - ) - Java Regex a-z, A-Z, 0-9 and (.)(_)(-) 如何将拉丁语unicode字符替换为[az]字符 - how to replace Latin unicode character to [a-z] characters 正则表达式:用连字符替换空格,只允许使用az - Regex: replace whitespaces with hyphens and allow only a-z 为什么带有正则表达式 ^[az]$ 的 find() 不等于带有正则表达式 [az] 的 match()? - Why is find() with Regex ^[a-z]$ not equivalent to matches() with Regex [a-z]? 正则表达式删除到t / aZ / - Regex to remove to t/a-Z/ Java正则表达式用下划线替换字符串中的所有特殊字符,同时考虑删除前导、尾随、多个下划线 - Java regex to replace all special characters in a String with an underscore also considering removing leading,trailing,multiple underscores Java:如何删除字符串中除az,数字和德语字符以外的所有字符 - Java : How to remove all characters in String except a a-z,digits and German characters REGEX az 0-9但不仅仅是数字 - REGEX a-z 0-9 but not only numbers 删除所有空格和所有非字母字符。 小写AZ的字符,转换为大写 - Remove all spaces and all non-alphabet characters. Characters that are A-Z in lower case, convert them to upper case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM