You want to use a regular expression to delete the previous character of a particular character.

Asked 2 years ago, Updated 2 years ago, 64 views

For example, when there is a character string ●N◆XY clock, the character XY is fixed, and ●N◆ takes various characters regardless of the symbol, and clock takes various kanji and hiragana.
At this time, I would like to delete the first letter up to the fixed XY.

I think I can do it with regular expressions, but I don't know how to delete them with regular expressions.
This may be a rudimentary question, but I would appreciate your cooperation.

python python3 regular-expression

2022-09-30 19:30

2 Answers

Wilwilco has answered himself, but r'^[^XY]*' does not seem to meet the requirements.
Because you have deleted any string that is not X or Y, for example:

>>re.sub(r'^[^XY]*',', '●N◆YYXY clock')
"YYXY watch".
>>re.sub(r'^[^XY]*',', '●N◆YYYX Clock')
"YYYX watch".
>>>re.sub(r'^[^XY]*', '', '●N◆X clock')
"X clock".
>>re.sub(r'^.*(XY.*)',r'\1', '●N◆XY clock')
"XY watch".
>>re.sub(r'^.*(XY.*)',r'\1', '●N◆YYXY Clock')
"XY watch".
>>re.sub(r'^.*(XY.*)',r'\1', '●N◆X clock')
'●N◆X clock'

Any string that starts with XY is captured and used as a replacement string in r'\1'.

>>re.sub(r'^.*(?=XY)',r', '●N◆YYXY clock')
"XY watch".
>>>re.sub(r'^.*(?=XY)',r', '●N◆XY clock')
"XY watch".
>>>re.sub(r'^.*(?=XY)',r', '●N◆X clock')
'●N◆X clock'

Use (?=...) to replace it with an empty string until just before XY.

Watch takes a variety of kanji and hiragana

This condition is not included in my reply.
Any characters other than kanji and hiragana are allowed.
Willwilco's answer is the same, so I'm going through it this time.
(Regular expressions that match only kanji and hiragana are a bit troublesome.)


2022-09-30 19:30

After much consideration, I might have been able to use the following regular expressions as expected.

import re

a=re.sub(r'^[^XY]*',', '●N◆XY clock')

print(a)


2022-09-30 19:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.