[英]iMacros - remove last character in string
I have a string like "673.35."我有一个像“673.35”这样的字符串。 defined as VAR3.定义为 VAR3。
I simply want to remove the last "."我只是想删除最后一个“。” so I am left with "673.35".所以我只剩下“673.35”。 It is proving very tricky for me.这对我来说非常棘手。 Using free version of iMacros plug-in on Google Chrome.在 Google Chrome 上使用免费版本的 iMacros 插件。
I cannot use split function or replace function I don't think as there there will always be a decimal point in my string.我不能使用拆分 function 或替换 function 我不认为我的字符串中总会有小数点。
SET !VAR3 EVAL("var s = '{{!VAR3}}'; var x,y,z; y = s.split('.'); z = y[1].split('.'); z[0];")
This is what I have tried but obviously it returns "35" as I have two decimal points in my string.这是我尝试过的,但显然它返回“35”,因为我的字符串中有两个小数点。
For solving such problems the "regular expressions" were invented.为了解决这些问题,发明了“正则表达式”。 I do not know if imacros can handle regular expressions, but since you added "java" as tag I provide here the solution how it would be written in java:我不知道 imacros 是否可以处理正则表达式,但是由于您添加了“java”作为标签,我在这里提供了解决方案,它将如何在 java 中编写:
String s = "673.35.";
String regexp = "^(.*)(\\.?)$";
Matcher m = Pattern.compile(regexp).matcher(s);
if ( m.matches() )
{
String result = m.group( 1);
System.out.println( "result: " + result );
}
else
{
System.out.println( "no match");
}
This regexp works for此正则表达式适用于
673.53. > 673.53
673.53 > 673.53
12345. > 12345
12345 > 12345
Regexp's seem to be very cryptic, but they are very powerful.正则表达式似乎很神秘,但它们非常强大。 Learn about it in the java doc of java.util.regex.Pattern.在 java.util.regex.Pattern 的 java 文档中了解它。 Here some explanations on above regexp: '^' is "start of line/string", '.*' is "any character(s), '\\.'这里对上述正则表达式进行一些解释:'^' 是“行/字符串的开头”,'.*' 是“任何字符,'\\.' is "one dot" (since '.' is "any character", the dot itself must be escaped by '\', used in a java string constant the '\' must also be escaped, therefore two '\'), '?'是“一个点”(因为“。”是“任何字符”,点本身必须用“\”转义,用于 java 字符串常量,“\”也必须转义,因此两个“\”),' ? is "once or none" (of preceding definition, here the dot) '$' is "end of line/string". The parantheses group the search result in order to access it afterwards ("m.group(1)").是“一次或没有”(前面的定义,这里是点)“$”是“行/字符串的结尾”。括号对搜索结果进行分组以便之后访问它(“m.group(1)”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.