[英]Single character followed by an O is deleted using regular expression
i need to match if a middle name is composed of the following characters, “JO”, the O will be deleted, leaving behind the “J”. 如果中间名由以下字符“ JO”组成,我需要匹配,O将被删除,而留下“ J”。 My regex is
standardName.replaceAll("(?<=[AZ])[O]*", "")
. 我的正则表达式是
standardName.replaceAll("(?<=[AZ])[O]*", "")
。 This is working but its replacing other strings too. 这是可行的,但它也可以替换其他字符串。 For eg if i pass SOO BOO i am getting as SB only if i get a single character followed by O like above O should be replaced else not.
例如,如果我通过SOO BOO,则仅当我得到单个字符后跟上面的O时,我才成为SB,否则应替换O。 Any suggestions?
有什么建议么?
You can use word boundary on both sides: 您可以在两侧使用单词边界:
standardName = standardName.replaceAll("(?<=\\b[A-Z])O\\b", "");
standardName.replaceAll("(?<=\\s[A-Z]|^[A-Z])[A-Z])[O]\\b", "")
This should do it for you. 这应该为您做。
\\b
is a word boundary which will not allow a letter after first O
. \\b
是一个单词边界,在第一个O
之后不允许出现字母。
or 要么
standardName.replaceAll("(?<=\\s[A-Z]|^[A-Z])O(?=\\s|$)", "")
You may try this, 你可以试试看
standardName.replaceAll("(?<=\\s[A-Z])O(?!\\S)", "");
Following negative lookahead asserts that the O
must not be followed by a non-space character. 否定的前瞻断言
O
后面不能带有非空格字符。
or 要么
standardName.replaceAll("\\s([A-Z])O\\s", "$1");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.