marlon
marlon

Reputation: 7633

How to do pattern replacement with regex?

I want to match 'windowsXP' in this string:

windowsXP|7|8 

Or

windowsXP 7 8

I wrote a pattern to match it:

 ([^0-9|])+[0-9\.]+.*

Why doesn't it match? It's the | making the trouble in the bracket. I tried escaping it but it didn't work either.

([^0-9\|])+[0-9\.]+.*

Upvotes: 2

Views: 60

Answers (3)

Ryszard Czech
Ryszard Czech

Reputation: 18611

Look:

  1. ([^0-9|])+ find characters different from digits and pipes
  2. [0-9\.]+ attempts matching a digit or a period, but there is a pipe that the expression cannot catch.

Use

^([^|\s]+)[|\s](\d+)[|\s](\d+)

See proof

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^|\s]+                  any character except: '|', whitespace (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  [|\s]                    any character of: '|', whitespace (\n, \r,
                           \t, \f, and " ")
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  [|\s]                    any character of: '|', whitespace (\n, \r,
                           \t, \f, and " ")
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \3

Python code:

import re
regex = r"^([^|\s]+)[|\s](\d+)[|\s](\d+)"
test_str = "windowsXP|7|8"
print(re.findall(regex, test_str))

Results: [('windowsXP', '7', '8')]

Upvotes: 2

anubhava
anubhava

Reputation: 784998

You may use this regex to math both cases:

^[^|\s]+

RegEx Demo

RegEx Details:

  • ^: Start
  • [^|\s]+: Match 1+ of any character that are not | and not whitespace

Upvotes: 1

shubhushan shambhu
shubhushan shambhu

Reputation: 41

It is rather simple. Just think of selecting the alphabets not negating numerics.

regex = \[a-z]*\i

Upvotes: -1

Related Questions