Python 3.x 如何在一个块中检查两个条件,而不是在python中检查两个条件

Python 3.x 如何在一个块中检查两个条件,而不是在python中检查两个条件,python-3.x,string,if-statement,replace,split,Python 3.x,String,If Statement,Replace,Split,所以我有这段代码,我想检查字符串变量job_title中是否包含“招聘”或“招聘”,然后通过拆分job_title得到这个单词后面的文本…我想在一个if语句中完成它…但现在我有两个…我怎么做呢,因为对于一个语句: if 'hiring ' in job_title: job_title = job_title.split("hiring ")[1] if job_title.startsw

所以我有这段代码,我想检查字符串变量job_title中是否包含“招聘”或“招聘”,然后通过拆分job_title得到这个单词后面的文本…我想在一个if语句中完成它…但现在我有两个…我怎么做呢,因为对于一个语句:

            if 'hiring ' in job_title:
                job_title = job_title.split("hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')
            elif 'Hiring ' in job_title:
                job_title = job_title.split("Hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')
            job_titles.append(job_title)

我必须在“雇用”或“雇用”时分开。。。有什么想法吗?

您可以使用以下条件。它利用了
参见行为示例。如果不需要空间,请使用strip()

            if 'hiring ' or 'Hiring ' in job_title:
                job_title = job_title.split("hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')
如果您想保留大小写,您可以修改函数以制作小写副本并签入该副本。见下文

>>> def get_text(text):
...     if (len(info:=text.lower().split('hiring'))>1):
...             return info[-1]
...     else:
...             return 'no hiring found'
... 


```>>> get_text('Example Co. is hiring example staff for example')
' example staff for example'
>>> get_text('Example Co. is HIRING example staff for example')
' example staff for example'
>>> get_text('Example Co. is example staff for example')
'no hiring found'
>>> get_text('HIRING is example staff for example')
' is example staff for example'
>>> get_text('example')
'no hiring found'
>>> get_text('')
'no hiring found'
>>> 

下面是一个使用正则表达式对字符串部分执行不区分大小写的替换的方法,直到并包括“雇用”的第一个实例,可选地后跟一个空格序列,可选地后跟一个破折号,可选地后跟另一个空格序列

这是一种模式:
r'(?i)^.*?雇用\s*-?\s*'

这就是它的作用:

>>> def second_method(text):
...     textcopy = text.lower()
...     if 'hiring' in textcopy:
...             return text[textcopy.index('hiring') + len('hiring'):]
... 
>>> second_method('something HIRing Some Thing YGGUN jb')
' Some Thing YGGUN jb'
>>> 
输出

例如员工示例。
例如,工作人员。
例如,工作人员。
例如,工作人员。
例如,工作人员。
例如,Example Co.就是Example员工。
招聘员工的例子。
与您的代码一致,最后一个测试字符串显示替换仅限于单词“招聘”的第一个实例。如果该单词出现在字符串的后面,则不考虑该单词。如果您希望替换应用到最后一个实例,您可以删除目标单词前面的非贪婪修饰符,即

import re

word = 'hiring'
pattern = rf'(?i)^.*?{word}\s*-?\s*'

test_strings = ['Example Co. is Hiring example staff for example.',
                'Example Co. is hiring example staff for example.',
                'Example Co. is HiRiNg example staff for example.',
                'Example Co. is Hiring - example staff for example.',
                'Example Co. is Hiring     -   example staff for example.',
                'Example Co. is example staff for example.',
                'Example Co. is Hiring example staff for hiring example.',
               ]

for s in test_strings:
    print(re.sub(pattern, '', s))

我想你会发现使用会更适合你的任务。我对python比较陌生,发现正则表达式非常难…有没有其他方法认为它们可能有点棘手,但它们绝对值得学习,相信我。观看一些教程,尝试一些实践练习,你就会意识到它们是多么有用。好的,对于这个例子,我希望在“招聘”或“招聘”后的字符串使用RE…例如“example Co.正在招聘example staff”。。。你能为此构造RE吗?我使用了这个模式:pattern=RE.compile(r'(?我不希望文本的其余部分是小写的…这就是问题所在…我尝试使用lower同时检查'Requiring'和'Requiring',但这也会将字符串的其余部分转换为小写,我不想复制并使用它。请参阅我添加的另一个函数中的示例。
>>> pattern = rf'(?i)^.*{word}\s*-?\s*'
>>> print(re.sub(pattern, '', test_strings[-1]))
example.