简体   繁体   中英

Regex-replace certains string with a range of numbers following after

Okay. So im dealing with cells where certain values need to be removed. In my comment in the code below there is two String examples. I want to create a String that can replace the string at the end with its 8 numbers too. Keep in mind that Im really looking for an algorithm that takes the 8 numbers right after "cvr" and not the text value at the end of a cell as this string could also be at the start.

//dontReplaceme 12345678 || cvr 87654321

String replaceCVRs = textValueForCPR.replaceAll("|CVR|Cvr|cvr|"+ (8 numbers after???))

You may use

String replaceCVRs = textValueForCPR.replaceAll("(?i)(cvr)\\s*\\d{1,8}", "$1")

NOTE : If you want to fail the match if there are more digits after an 8-digit number add (?!\d) lookahead at the end, "(?i)(cvr)\\s*\\d{1,8}(?!\\d)" .

See the regex demo here .

Details

  • (?i) - case insensitive mode on
  • (cvr) - Group 1: cvr string
  • \s* - 0 or more whitespaces
  • \d{1,8} - one to eight digits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM