Regular expression example question. (Except specific words from sentences)

Asked 1 years ago, Updated 1 years ago, 55 views

For example, a for b of c When there's this sentence, I want to extract the word "for" before and after.

Let's say the function we extract is func func("a for b of c", "for", 1) -> a func("a for b of c", "for", 2) -> b I'm trying to make sure that's what I'm trying to do.

If you know the oracle version, please reply.

Thank you.

** I don't know how to change the line ㅠ<

regex

2022-09-22 14:48

1 Answers

You can use the groupings.

(.*) for (.*)

Interpreted by

(Any letter or group with 0 or more in front of for) for (Any letter or group with 0 or more after for)

This is.

If you look at the Python code, you can write it as follows.

import re

search_target = 'shgdklahslktdgjwaolsdtgaswd for waseghtiouwaegtyawehtoi'
regex = '(.*) for (.*)'
r = re.search(regex, search_target)

if r:
    print(r.group(1))
    print(r.group(2))

If you press "Run" and run it on the code executor, you will see two lines printed out. You can use either group (1) or group (2).


2022-09-22 14:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.