[Tutor] regular expressions query
Alan Gauld
alan.gauld at yahoo.co.uk
Fri May 24 05:41:22 EDT 2019
On 24/05/2019 01:15, mhysnm1964 at gmail.com wrote:
> Below I am just providing the example of what I want to achieve, not the
> original strings that I will be using the regular expression against.
While I'm sure you understand what you want I'm not sure I do.
Can you be more precise?
> The original strings could have:
> "Hello world"
> "hello World everyone"
> "hello everyone"
> "hello world and friends"
> I have a string which is "hello world" which I want to identify by using
> regular expression how many times:
>
> * "hello" occurs on its own.
Define "on its own" Is the answer for the strings above 4?
Or is it 1 (ie once without an accompanying world)?
> * "Hello world" occurs in the list of strings regardless of the number
> of white spaces.
I assume you mean the answer above should be 3?
Now for each scenario how do we treat
"helloworldeveryone"?
"hello to the world"
"world, hello"
"hello, world"
> Splitting the string into an array ['hello', 'world'] and then re-joining it
> together and using a loop to move through the strings does not provide the
> information I want. So I was wondering if this is possible via regular
> expressions matching?
It is probably possible by splitting the strings and searching,
or even just using multiple standard string searches. But regex is
possible too. A lot depends on the complexity of your real
problem statement, rather than the hello world example
you've given. I suspect the real case will be trickier
and therefore more likely to need a regex.
> Modifying the original string is one option. But I was wondering if this
> could be done?
I'm not sure what you have in mind. For searching purposes
you shouldn't need to modify the original string. (Of course
Python strings are immutable so technically you can never
modify a string, but in practice you can achieve the same
effect.)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list