提取单词后,如何获得剩余部分?

时间:2019-03-28 09:43:53

标签: java android regex string substring

嗨,我有以下字符串

public enum MyEnum {
    A("Approved"),
    R("Reject New"),

    private String status;

    private MyEnum(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    public static MyEnum fromString(String text) {
        return Arrays.stream(values())
          .filter(v -> v.status.equalsIgnoreCase(text))
          .findFirst()
          .orElse(null);
    }
}

现在我想做的是,我想展示

“ << strong> am >”(粗体)和“ Cena ”(蓝色)。

我能够提取这些单词,但其余部分类似“嗨,我约翰”

如何获取?我找不到解决方法。

public class Demo {
    public static void main(String[] args) {
        System.out.println(MyEnum.fromString("A"));
    }
}

2 个答案:

答案 0 :(得分:0)

请使用以下代码

String mytext="Hi I <am> John (Cena)";
Pattern ptn = Pattern.compile("\\s+");
Matcher mtch = ptn.matcher(mytext);
mytext = mtch.replaceAll(",");
System.out.println("Array"+mytext.split(",")[0]);//[1][2] to get next elements in array

答案 1 :(得分:0)

Option Explicit

Public Sub DifferentOrSame()
    'read and split data sets into array
    Dim DataSet1 As Variant
    DataSet1 = Split(ThisWorkbook.Worksheets("data").Range("A2").Value, ",")

    Dim DataSet2 As Variant
    DataSet2 = Split(ThisWorkbook.Worksheets("data").Range("B2").Value, ",")

    Dim AllButUniqueItems As Object
    Set AllButUniqueItems = CreateObject("Scripting.Dictionary")

    Dim DuplicateItems As Object
    Set DuplicateItems = CreateObject("Scripting.Dictionary")

    Dim itm As Variant
    'process data set 1
    For Each itm In DataSet1
        If AllButUniqueItems.Exists(itm) Then
            If Not DuplicateItems.Exists(itm) Then
                DuplicateItems.Add itm, 1
            End If
        Else
            AllButUniqueItems.Add itm, 1
        End If
    Next itm

    'process data set 2
    For Each itm In DataSet2
        If AllButUniqueItems.Exists(itm) Then
            If Not DuplicateItems.Exists(itm) Then
                DuplicateItems.Add itm, 1
            End If
        Else
             AllButUniqueItems.Add itm, 1
        End If
    Next itm

    'concatenate strings
    Dim StrDuplicates As String, StrUniques As String, StrAllButUnique As String
    For Each itm In AllButUniqueItems 
        StrAllButUnique = StrAllButUnique & IIf(StrAllButUnique <> vbNullString, ",", "") & itm

        If Not DuplicateItems.Exists(itm) Then
            StrUniques = StrUniques & IIf(StrUniques <> vbNullString, ",", "") & itm
        Else
            StrDuplicates = StrDuplicates & IIf(StrDuplicates <> vbNullString, ",", "") & itm
        End If
    Next itm

    'write to cells
    ThisWorkbook.Worksheets("data").Range("C2").Value = "'" & StrDuplicates
    ThisWorkbook.Worksheets("data").Range("D2").Value = "'" & StrUniques
    ThisWorkbook.Worksheets("data").Range("E2").Value = "'" & StrAllButUnique
End Sub

查看this链接以获取更多示例