首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用变量替换Java匹配

用变量替换Java匹配
EN

Stack Overflow用户
提问于 2014-09-10 15:10:03
回答 2查看 122关注 0票数 0
代码语言:javascript
复制
/* Look for phone numbers starting with a 0 after a space
character and replace the 0 with "+44 (0)" */

    import java.util.regex.*;

    public class Reg6 {

        public static void main (String [] args) {

            String variable1 = "test";
            Pattern phone = Pattern.compile("\\s0");
            Matcher action = phone.matcher(args[0]);
            String worldwide = action.replaceAll(" +44 (0) ");
            System.out.println(worldwide);

        }
    }

如何将variable1放置到replaceAll方法中?如果有一个字符串{$variable_1},我希望用我的variable_1替换它,这可能吗?

在php模板中类似于

"{$test}“-我希望使用与字符串中匹配的名称,并找到与此字符串匹配的变量,并将其放在那里,将其全部替换为此变量值。

就像在模板中一样

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-10 15:23:16

您不必使用模式或匹配,只需尝试以下操作

代码语言:javascript
复制
    String number ="028346";
    if(number.startsWith("0"))
        System.out.println("+44 (0)" + number.substring(1));
票数 0
EN

Stack Overflow用户

发布于 2014-09-10 15:24:42

也许你想要这样

代码语言:javascript
复制
Pattern varMatch = Pattern.compile("\\{\\$variable_1\\}");
Matcher action = varMatch.matcher(worldwide);
worldwide = action.replaceAll(variable1);
System.out.println(worldwide);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25768987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档