Regex to match all non-digit characters which are

2019-06-14 09:51发布

Using SQL Anywhere 16 regex (Perl 5 equivalent) I would like to match all non-digit characters in the following string:

This 100 is 2 a 333 test. such that the match results in This is a test.

I have a feeling this requires lookarounds but I'm not sure of the exact syntax.

Some of my failed attempts:

\D* matches This

\D*(?=\d) matches This

(\D*(?=\d*)|(?<=\d)\D*)* matches This

1条回答
仙女界的扛把子
2楼-- · 2019-06-14 10:03

I take it that the typical 'search-and-replace' functionality cannot be used in this case, based on added clarifications (and my own search). It also appears that the typical "global" matching is not available. Given that, some remaining options are

  • The "global matching" can be done with look-around assertions, per Documentation.

Here is an example from SQL Anywhere 12 docs, pulling words out of a string with numbers

Lookahead and lookbehind assertions can be useful with REGEXP_SUBSTR when trying to split a string. For example, you can return the list of street names (without the street numbers) in the Address column of the Customers table by executing the following statement:

SELECT REGEXP_SUBSTR( Street, '(?<=^\\S+\\s+).*$' ) FROM Customers;
  • Compose the pattern to capture and match along. Should be
    (\D+)\d*(\D+)\d* ...

The problem is that you'd have to manually provide enough of these to cover the whole string.

  • The REGEXP_SUBSTR allows a specification of the occurrence to match (first, fourth, ...).
    One could then write a loop to match simple \D+, with increasing position through the loop.

However, loops are generally not easily recommended.

查看更多
登录 后发表回答