x2ph
x2ph

Reputation: 104

RegEx for matching everything except words starting with a plus sign

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?

Upvotes: 0

Views: 104

Answers (4)

x2ph
x2ph

Reputation: 104

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

Upvotes: 0

Toto
Toto

Reputation: 91385

Use:

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

Demo

Upvotes: 3

kerwei
kerwei

Reputation: 1842

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/

Upvotes: 0

user557597
user557597

Reputation:

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.

Upvotes: 1

Related Questions