简体   繁体   English

iMacros - 删除字符串中的最后一个字符

[英]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.

相关问题 正则表达式删除最后一个/如果它作为字符串中的最后一个字符存在 - Regex to remove last / if it exists as the last character in the string 删除数组中每个字符串的最后一个字符 (JavaScript) - Remove the last character of each string in array (JavaScript) Javascript - 如果URL字符串的最后一个字符是“+”,那么删除它......怎么样? - Javascript - If Last Character of a URL string is “+” then remove it…How? 如何删除字符串的第一个和最后一个字符 - How to remove the first and the last character of a string 如何从While循环中的最后一个字符串中删除最后一个字符? - How to remove the last character from last string in a While loop? 如何在javascript imacros中删除字符串的随机部分 - How to remove random part of string in javascript imacros 如何使用正则表达式删除带有imacros eval的文本中的最后一个句子 - How to remove last sentence in text with imacros eval using regex 如何从包含在 html 元素中的字符串中删除最后一个字符? - How to remove the last character from a string contained within an html element? 使用For-Loop从字符串中删除最后一个字符(如果是“!”)-JavaScript - Remove Last Character From String If It's a “!” Using For-Loop - JavaScript 在最后一次出现特定字符后,如何删除字符串的特定部分? - How to remove a specific section of a string after the last occurrence of a specific character?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM