简体   繁体   中英

Replace all occurrences of specific character in the first line of text with regex

I have to grab the character ; in the first line of a TXT.

Example of TXT:

abcd ; 123 ; 05/03/2020 ; maria ; 1050126584 ; efghijkl- ; 00000 
bcda ; 321 ; 22/12/1920 ; kyle  ; 0123514826 ; isaadgfa- ; 11111

I have to take the ; only in the first line.

I know that the expression (?<!\\s+)^(.+)$ can catch only the first line, but I can't filter this line.

It's for a Java application, without code access.

You can use

\G([^;\r\n]*);

Replace with $1 if you need to remove the semi-colons, or $1<NEW_TEXT> if you need to replace with some NEW_TEXT .

See the regex demo .

Details :

  • \\G - start of string or the end of the previous successful match
  • ([^;\\r\\n]*) - Group 1 ( $1 in the replacement refers to the group value): any zero or more chars other than ; , CR and LF chars
  • ; - a semi-colon.

Note that if you need to deal with any Unicode line break chars, you can use

\G([^;\r\n\x0B\x0C\u0085\u2028\u2029]*);

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