使用正则表达式获取子字符串

huangapple go评论90阅读模式
英文:

Get substring using regex

问题

我有一些字符串,类似于:

  1. IND_FROM_ONE_TO_FIVE
  2. IND_FROM_FIVE_TO_TEN
  3. BS_FROM_ONE_TO_FIVE
  4. BS_FROM_FIVE_TO_TEN
  5. OP_FROM_ONE_TO_FIVE
  6. OP_FROM_FIVE_TO_TEN

我想从它们所有的字符串中剪切掉第一个“_”及其之前的内容,但保留之后的所有内容。

类似于:

  1. IND_FROM_ONE_TO_FIVE => FROM_ONE_TO_FIVE
  2. IND_FROM_FIVE_TO_TEN => FROM_FIVE_TO_TEN
  3. BS_FROM_ONE_TO_FIVE => FROM_ONE_TO_FIVE
  4. BS_FROM_FIVE_TO_TEN => FROM_FIVE_TO_TEN 等等。

我尝试过 /[^_]*/,但它返回 IND_FROM_ONE_TO_FIVE => _FROM_ONE_TO_FIVE(没有剪切第一个“_”)。

我应该如何在 Java 中实现这个?

英文:

I have some strings Like

1.IND_FROM_ONE_TO_FIVE  
2.IND_FROM_FIVE_TO_TEN  
3.BS_FROM_ONE_TO_FIVE 
4.BS_FROM_FIVE_TO_TEN   
5.OP_FROM_ONE_TO_FIVE  
6.OP_FROM_FIVE_TO_TEN

And I want to cut from all of them everything before the first "" include ""!!!.

Something like :

1.IND_FROM_ONE_TO_FIVE => FROM_ONE_TO_FIVE

2.IND_FROM_FIVE_TO_TEN => FROM_FIVE_TO_TEN

3.BS_FROM_ONE_TO_FIVE => FROM_ONE_TO_FIVE

4.BS_FROM_FIVE_TO_TEN => FROM_FIVE_TO_TEN etc.

I have tried /[^_]*/ but it returns IND_FROM_ONE_TO_FIVE => _FROM_ONE_TO_FIVE (did not cut first "_")

How could I make it on java?

答案1

得分: 2

你可以在" is" 之前添加锚点,并且也可以匹配下划线。在替换中使用空字符串。

^[^_]*_

正则表达式演示 | Java 示例

使用 replaceFirst 时可以省略锚点:

System.out.println("IND_FROM_ONE_TO_FIVE".replaceFirst("[^_]*_", ""));

输出:

FROM_ONE_TO_FIVE
英文:

You can prepend an anchor and match the underscore after is as well. In the replacement use an empty string.

^[^_]*_

Regex demo | Java demo

Using replaceFirst you can omit the anchor:

System.out.println("IND_FROM_ONE_TO_FIVE".replaceFirst("[^_]*_", ""));

Output

FROM_ONE_TO_FIVE

答案2

得分: 1

Sure, here's the translated content:

你可以使用捕获组来提取你想要的子字符串。

    ^[^_]+_(.*)

我还尝试在Java上测试了结果。

import java.util.regex.*;
public class MyClass {
    public static void main(String args[]) {
        String mydata = "BS_FROM_FIVE_TO_TEN";
        Pattern pattern = Pattern.compile("^[^_]+_(.*)");
        Matcher matcher = pattern.matcher(mydata);
        if (matcher.find())
        {
            System.out.println(matcher.group(1));
        }
    }
}

结果

FROM_FIVE_TO_TEN
英文:

You could use a capturing group to extract substring that you want to.

^[^_]+_(.*)

I also tried to test result on Java.

import java.util.regex.*;
public class MyClass {
    public static void main(String args[]) {
        String mydata = "BS_FROM_FIVE_TO_TEN";
        Pattern pattern = Pattern.compile("^[^_]+_(.*)");
        Matcher matcher = pattern.matcher(mydata);
        if (matcher.find())
        {
            System.out.println(matcher.group(1));
        }
    }
}

Result

FROM_FIVE_TO_TEN

答案3

得分: 0

你可以使用匹配而不是替换:从此正则表达式(多行)中获取第一个且唯一的捕获组:^\d+\.[^_]+_(.*)$(https://regex101.com/r/Y3Ztv4/1)。

英文:

You can use matching rather than replacing: take the first and only capture from this regex (multiline): ^\d+\.[^_]+_(.*)$ (https://regex101.com/r/Y3Ztv4/1).

huangapple
  • 本文由 发表于 2020年10月6日 16:10:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64221799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定