【问题标题】:Regex to get everything but last chars of capture group正则表达式获取捕获组的最后一个字符以外的所有内容
【发布时间】:2022-12-04 12:28:27
【问题描述】:

How do I use regex to select everything before last 4 char in a capture group?

Example:

String str = \"{Index1=StudentData(studentName=Sam, idNumber=321231312), Index2=StudentData(studentName=Adam, idNumber=5675), Index3=StudentData(studentName=Lisa, idNumber=67124)}\";
String regex = \"(?<=idNumber=)[a-zA-Z1-9]+(?=\\))\";

System.out.println(str.replaceAll(regex, \"*\"));

Current output:

{Index1=StudentData(studentName=Sam, idNumber=*), Index2=StudentData(studentName=Adam, idNumber=*), Index3=StudentData(studentName=Lisa, idNumber=*)}

Desired output:

{Index1=StudentData(studentName=Sam, idNumber=*****1312), Index2=StudentData(studentName=Adam, idNumber=5675), Index3=StudentData(studentName=Lisa, idNumber=*7124)

    标签: java regex regex-lookarounds regex-group


    【解决方案1】:
    【解决方案2】:
    【解决方案3】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2020-08-15
    • 2022-06-15
    • 2011-08-24
    • 2013-11-15
    相关资源
    最近更新 更多