1

I want a string between (comma or whitespace) and a string.

The code I am using is

re.findall('[,]?[\s]*(.*?)[.]calc', string)

The output I get on running this

String 1 = "Select ID, Deliveryid.calc"
Result = Deliveryid
String 2 = "Select Deliveryid.calc"
Result = Select Deliveryid.calc

Now For string two I just want the Deliveryid and not the hole. Basically I want the string before ".calc" and "whitespace or comma". It can be anyone or both in the case of comma and whitespace

There can be a newline or tab also. Basically any type of whitespace along with the comma. Both can be present or anyone.

FYI - I want all the matching strings in the string. there can be one or many

9
  • Do you mean [ .]([^ .]*)\.calc / [\s.]([^\s.]*)\.calc (demo)? Or simply [\s.](\w+)\.calc? Commented Nov 10, 2020 at 10:56
  • The problem is with what space or comma to start matching from or what (.*?) can match. Please clarify these points. Commented Nov 10, 2020 at 11:06
  • Well, yes, I used a . instead of a , in the above comment. Still, the question remains. Commented Nov 10, 2020 at 11:22
  • 1
    Please still edit to show a good example of an input string with multiple occurrences, and the expected output for that. Commented Nov 10, 2020 at 12:27
  • 1
    So if [\s,]([^\s,]*)\.calc enough? See demo. Commented Nov 10, 2020 at 14:32

3 Answers 3

1

You can use

[\s,]([^\s,]*)\.calc

See the regex demo. Details:

  • [\s,] - a whitespace or comma
  • ([^\s,]*) - Group 1: any zero or more chars other than whitespace and comma
  • \.calc - a .calc string.

See the Python demo:

import re
text = 'Select ID, Deliveryid.calc Select ID, Deliveryid.calc Select ID, Deliveryid.calc\nSelect Deliveryid.calc Select Deliveryid.calc'
print( re.findall(r'[\s,]([^\s,]*)\.calc', text) )
# => ['Deliveryid', 'Deliveryid', 'Deliveryid', 'Deliveryid', 'Deliveryid']
Sign up to request clarification or add additional context in comments.

Comments

0

Modified the regex slightly

import re
String1 = "Select ID, Deliveryid.calc"
String2 = "Select Deliveryid.calc"
m1 = re.search('[, ]([^, ]*?)[.]calc', String2)
print ('->' + m1.group(0) + '<-')
print ('->' + m1.group(1) + '<-')

I think you should get value of group(1).

2 Comments

Why findall is not working as I am getting \t\n while using findall but when using match it gives the correct answer. And how to get the list and not the object while using match?
And there can be multiple matching strings in that. So how get the list for all.
0

You have to use greedy regex operators. try this:

^[^.]+

It will match everything before the dot Note: that when using greedy operators, you'll match everything before the last .(dot) instead of the first. As per your string it matches everything before the .calc

enter image description here

If you want only a word before .(dot) then "Wiktor Stribiżew" has already helped you out for that. His expression works for me similar way. enter image description here

Happy Learning !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.