简体   繁体   中英

Regex Word Boundary including hyphen

With the regular expression \\s{2}\\b I'm trying to replace the last two whitespaces before a word with a pipe (|) using a Word Boundary, but it's ignoring the number -18.055,81 . With what kind of expression I can also get the last two whitespaces before -18.055,81 ?

Example:

1234 - This is a test        18.055,81        -18.055,81        0,00        0,00        18.055,81

Results in:

1234 - This is a test       |18.055,81        -18.055,81       |0,00       |0,00       |18.055,81

What I want:

1234 - This is a test       |18.055,81       |-18.055,81       |0,00       |0,00       |18.055,81

You can use

\s{2}(?=-?\d)
\s{2}(?=-?\w)

See the regex demo . Details:

  • \\s{2} - two whitespace chars
  • (?=-?\\d) - that are immediately followed with an optional hyphen and then a digit.

If there are any other word chars expected (letters, or underscores), replace \\d with \\w .

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