I have a string like this:
我有一个像这样的字符串:
String str = ${farsiName} - {symbolName}
I want to use split method to find and extract farsiName & symbolName from this string with regex. I found this solution https://*.com/a/4006273/2670847 for doing something like this:
我想使用split方法从此字符串中使用regex查找和提取farsiName和symbolName。我发现这个解决方案https://*.com/a/4006273/2670847做了这样的事情:
String in = "Item(s): [item1.test],[item2.qa],[item3.production]";
Pattern p = Pattern.compile("\\[(.*?)\\]");
Matcher m = p.matcher(in);
while(m.find()) {
System.out.println(m.group(1));
}
But I want to know, can I use similar regex for split method in String class?
但我想知道,我可以在String类中使用类似的正则表达式来实现split方法吗?
3 个解决方案
#1
0
You are on right track. Just replace the brackets and string.
你走在正确的轨道上。只需更换括号和字符串即可。
String n ="${farsiName} - {symbolName}";
Pattern p = Pattern.compile("\\{(.*?)\\}");
Matcher m = p.matcher(n);
while(m.find()) {
System.out.println(m.group(1));
}
#2
0
Note : I do not recommend this. A regex would be much more versatile, and powerful.
注意:我不推荐这个。正则表达式将更加通用,功能强大。
String n ="${farsiName} - {symbolName}";
String s[] = n.split(" - ");
for(String x : s){
System.out.println(x.replace("$", "").replace("{", "").replace("}", ""));
}
#3
0
public static void main(String[] args){
String n ="${farstName} - {symbolName}";
String arr[] = n.split(" - ");
for(String s : arr){
System.out.println(s.replace("$", "").replace("{", "").replace("}", ""));
}
}
Use this code.It will work.But I recommend regex.
使用此代码。它会工作。但我推荐正则表达式。
#1
0
You are on right track. Just replace the brackets and string.
你走在正确的轨道上。只需更换括号和字符串即可。
String n ="${farsiName} - {symbolName}";
Pattern p = Pattern.compile("\\{(.*?)\\}");
Matcher m = p.matcher(n);
while(m.find()) {
System.out.println(m.group(1));
}
#2
0
Note : I do not recommend this. A regex would be much more versatile, and powerful.
注意:我不推荐这个。正则表达式将更加通用,功能强大。
String n ="${farsiName} - {symbolName}";
String s[] = n.split(" - ");
for(String x : s){
System.out.println(x.replace("$", "").replace("{", "").replace("}", ""));
}
#3
0
public static void main(String[] args){
String n ="${farstName} - {symbolName}";
String arr[] = n.split(" - ");
for(String s : arr){
System.out.println(s.replace("$", "").replace("{", "").replace("}", ""));
}
}
Use this code.It will work.But I recommend regex.
使用此代码。它会工作。但我推荐正则表达式。