I have a string which contains the following information.
mystring = "'$1$Not Running', ''"
I want to be able to remove the extra space and , '' after the Running. I tried to use strip() but it does not seem to work.
My desired output is mystring = "'$2$Not Running'"
I am not sure what I am missing here? Any help is appreciated.
One of the easier solutions would be to partition your string based on the comma:
mystring, comma, rest = mystring.partition(",")
This solution depends on there not being any commas in the string other than that one.
The better solution would be to figure out why the extra characters are in your string and what you can do to avoid it.
If that isn't possible, it looks like the string is valid Python, so you could parse it as a tuple and always pick the first element:
import ast
mystring, _ = ast.literal_eval(mystring)
Although in this case you would get what's inside the single quotes, not the single quotes as characters themselves.
i assume you want to remove the final 4 char's in your string. To do this you can simply
mystring = mystring[:-4]
if this is not right tell me and ill try to find a solution
strip() only removes spaces as the beginning and end of a string. Since what you want to remove is in the middle, it won't work for you.
You can use regular expressions to search and replace for specific strings:
import re
mystring = "'$1$Not Running', ''"
mynewstring = re.sub(", ''", "", mystring)
print(mynewstring)
# '$1$Not Running'
I'm not sure what extra space you're talking about, but you can use similar logic to replace it.
If this is literally the only thing you need it for, then some of the other answers might be simpler. If you need it for several different cases of input, this might be a better option. We'd need to see more examples of input to figure that out though.
Maybe there is something better but you can try to use split()
mynewstring = mystring.split()[0] + mystring.split()[1]
If the 4 characters you want to replace are ', ' then you can just use the string.replace() function to replace them with an empty string '':
mystring = mystring.replace( "', '", '')
Related
I am new to regexes.
I have the following string : \n(941)\n364\nShackle\n(941)\nRivet\n105\nTop
Out of this string, I want to extract Rivet and I already have (941) as a string in a variable.
My thought process was like this:
Find all the (941)s
filter the results by checking if the string after (941) is followed by \n, followed by a word, and ending with \n
I made a regex for the 2nd part: \n[\w\s\'\d\-\/\.]+$\n.
The problem I am facing is that because of the parenthesis in (941) the regex is taking 941 as a group. In the 3rd step the regex may be wrong, which I can fix later, but 1st I needed help in finding the 2nd (941) so then I can apply the 3rd step on that.
PS.
I know I can use python string methods like find and then loop over the searches, but I wanted to see if this can be done directly using regex only.
I have tried the following regex: (?:...), (941){1} and the make regex literal character \ like this \(941\) with no useful results. Maybe I am using them wrong.
Just wanted to know if it is possible to be done using regex. Though it might be useful for others too or a good share for future viewers.
Thanks!
Assuming:
You want to avoid matching only digits;
Want to match a substring made of word-characters (thus including possible digits);
Try to escape the variable and use it in the regular expression through f-string:
import re
s = '\n(941)\n364\nShackle\n(941)\nRivet\n105\nTop'
var1 = '(941)'
var2 = re.escape(var1)
m = re.findall(fr'{var2}\n(?!\d+\n)(\w+)', s)[0]
print(m)
Prints:
Rivet
If you have text in a variable that should be matched exactly, use re.escape() to escape it when substituting into the regexp.
s = '\n(941)\n364\nShackle\n(941)\nRivet\n105\nTop'
num = '(941)'
re.findall(rf'(?<=\n{re.escape(num)}\n)[\w\s\'\d\-\/\.]+(?=\n)', s)
This puts (941)\n in a lookbehind, so it's not included in the match. This avoids a problem with the \n at the end of one match overlapping with the \n at the beginning of the next.
I am using Python to build the logic. Facing the challenge in removing the whitespaces in the string.
Actual code as below:
searchExprLineWithoutSpace.replace(" ", "").strip())
Even i have triedwith lstrip, rstrip but no luck. Actual String in this is as below:
"// SetSearchExpr(searchExpr);
ExecuteQuery();
var FR = FirstRecord();"
Not matter what I do, I am unable to remove the space from the first line:
If someone can steer as to what else I can do, it would help. Thanks.
If you want to remove all whitespace, you can use the generic paramter for str.split, then join it.
One thing I will note is that this string that you provided is incorrect, since it needs to be triple quote for multilines.
Anyways, if you want to remove the whitespace, you can do
white_space_to_remove = "hello world this is uneven whitespace"
white_space_to_remove = ''.join(white_space_to_remove.split())
This will give the string with no whitespace whatsoever, since the delimiter for str.join is just an empty string.
However, I'm unsure as to what you're trying to do with removing whitespace since your question was quite generic.
Note:
str.split without any parameters will by default split by any whitespace.
I have a string = "msdjdgf(^&%*(Aroha Technologies&^$^&*^CHJdjg" with special characters.
what i am trying is to remove all special charecters in the string and then display the word 'Aroha Technologies'
i was able to do with hard coding using lstrip() function but can anyone help me out how can i display string 'Aroha Technologies' in a single line using regular expressions.
edit suggested:-
by using this lstrip() and rstrip() functions i was able to remove characters from the string.
str = "msdjdgf(^&%*(Aroha Technologies&^$^&*^CHJdjg"
str=str.lstrip('msdjdgf(^&%*(')
str=str.rstrip('&^$^&*^CHJdjg')
here, A bit more dirty approach
import re # A module in python for String matching/operations
a = "msdjdgf(^&%*(Aroha Technologies&^$^&*^CHJdjg"
stuff = re.findall('\W(\w+\s\w+)\W', a)
print(stuff[0]) # Aroha Technologies
hope this helps ;)
You don't provide a lot of information, so this may or may not be close to what you want:
import re
origstr = "msdjdgf(^&%(Aroha Technologies&^$^&^CHJdjg"
match = re.search("[A-Z][a-z]*(?: [A-Z][a-z]*)*", origstr)
if match:
newstr = match.group()
(looks for a series of capitalized words with spaces between them)
I'd like to replace " and -
with "" nothing! make it disappear.
s = re.sub(r'[^\w\s]', '', s) this makes all punctuation disappear, but I just want those 2 characters. Thanks.
I'm curious as to why you are using a regular expression for this simple string replacement. The only advantage that I can see is that you can do it in one line of code instead of two, but I personally think that a replacement method is clearer than a regex for something like this.
The string object has a replace method - str.replace(old, new[, count]), so use replace("-", "") and replace("\"", "").
Note that my syntax might be a little off - I'm still a python beginner.
re.sub('["-]+', '', s)
In Python 2.6/2.7, you can use the helpful translate() method on strings. When using None as the first argument, this method has the special behavior of deleting all occurences of any character in the second argument.
>>> s = 'No- dashes or "quotes"'
>>> s.translate(None, '"-')
'No dashes or quotes'
Per SilentGhost's comment, this gets to be cumbersome pretty quickly in both <2.6 and >=3.0, because you have to explicitly create a translation table. That effort would only be worth it if you are performing this sort of operation a great deal.
re.sub('[-"]', '', s)
In Python 2.6:
print 'Hey -- How are "you"?'.translate(None, '-"')
Returns:
Hey How are you?
Might be a bit messy title, but the question is simple.
I got this in Python:
string = "start;some;text;goes;here;end"
the start; and end; word is always at the same position in the string.
I want the second word which is some in this case. This is what I did:
import re
string = "start;some;text;goes;here;end"
word = re.findall("start;.+?;" string)
In this example, there might be a few things to modify to make it more appropriate, but in my actual code, this is the best way.
However, the string I get back is start;some;, where the search characters themselves is included in the output. I could index both ;, and extract the middle part, but there have to be a way to only get the actual word, and not the extra junk too?
No need for regex in my opinion, but all you need is a capture group here.
word = re.findall("start;(.+?);", string)
Another improvement I'd like to suggest is not using .. Rather be more specific, and what you are looking for is simply anything else than ;, the delimiter.
So I'd do this:
word = re.findall("start;([^;]+);", string)