Sample string: +Z XA( 0,1,6,22,26,33,34,35,36,25,24) +DD +I +M >x1 >bdz +A

Result should be: +Z +DD +I +M +A or Z DD I M A

The regex (\+\w+) seems to find all tokens, but I don't know how to define the replace pattern. I'd like to remove everything else.

I use Omnis7 but the regex engine is a compiled Delphi library that I used to extend Omnis7. The regex engine is a full implementation of regex. I tried to find the regex with an online regex tool (regex101.com).

How do I solve this problem?

4 Answers

4
Toto On Best Solutions

Use:

  • Find: (\+\w+\h?)|.
  • Replace: $1

Demo

2
sln On

This is one way to do it

Find (?:(?!\+\w)[\S\s])*\+(\w+)(?:(?!\+\w)[\S\s])*
Replace $1

https://regex101.com/r/MAW3fT/1

Expanded

 (?:
      (?! \+ \w )
      [\S\s] 
 )*
 \+ 
 ( \w+ )                       # (1)
 (?:
      (?! \+ \w )
      [\S\s] 
 )*

While the other answer looks simple it is 2 times slower than this one.

Benchmarks

Regex1:   (\+\w+\h?)|.+?
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   49
Elapsed Time:    2.06 s,   2056.43 ms,   2056428 µs
Matches per sec:   1,191,386


Regex2:   (?:(?!\+\w)[\S\s])*\+(\w+)(?:(?!\+\w)[\S\s])*
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   5
Elapsed Time:    1.00 s,   997.28 ms,   997281 µs
Matches per sec:   250,681

What to notice is they both matched correctly, so the elapsed time
is the bell weather here. Where my regex took half the time.

0
kerwei On

Just managed to settle on a potential pattern but saw that you have already found your answer. Here's a suggestion nevertheless.

Find: (?<![+\w])[^+][\w\W]+?\s

Replace: ''

https://regex101.com/r/cXceQO/1/

0
x2ph On

Thanks to all here. I played around with it and found the exact solution:
Search: +Z XA( 0,1,6,22,26,33,34,35,36,25,24) +DD +I +M >x1 >bdz +A
Find: (\+)(\w+\h?)(.?)|.

  1. Replace: $1$2$3
    Result: +Z +DD +I +M +A

or

  1. Replace: $2$3
    Result: Z DD I M A