Skip to content
Advertisement

Tag: regex

parsing a .srt file with regex

I am doing a small script in python, but since I am quite new I got stuck in one part: I need to get timing and text from a .srt file. For example, from I need to get: 00:00:01,000 –> 00:00:04,074 and Subtitles downloaded from www.OpenSubtitles.org. I have already managed to make the regex for timing, but i am stuck

How to matches anything except space and new line?

I have a string, I just want to match string for any character except for space and new line. What must be regular expression for this? I know regular expressions for anything but space i.e. [^ ]+ and regular expression for anything but new line [^n]+ (I’m on Windows). I am not able to figure how to club them together.

Python, remove all non-alphabet chars from string

I am writing a python MapReduce word count program. Problem is that there are many non-alphabet chars strewn about in the data, I have found this post Stripping everything but alphanumeric chars from a string in Python which shows a nice solution using regex, but I am not sure how to implement it I’m afraid I am not sure how

Get the part of the string matched by regex

In the case of re.search(), is there a way I can get hold of just the part of input string that matches the regex? i.e. I just want the “heeehe” part and not the stuff that comes before it: Answer match.group(0) is the matched string. Demo: You can also omit the argument, 0 is the default.

How to check if a string is a valid regex in Python?

In Java, I could use the following function to check if a string is a valid regex (source): Is there a Python equivalent of the Pattern.compile() and PatternSyntaxException? If so, what is it? Answer Similar to Java. Use re.error exception: exception re.error Exception raised when a string passed to one of the functions here is not a valid regular expression

Remove Last instance of a character and rest of a string

If I have a string as follows: foo_bar_one_two_three Is there a clean way, with RegEx, to return: foo_bar_one_two? I know I can use split, pop and join for this, but I’m looking for a cleaner solution. Answer Which behaves like this: See in the documentation entry for str.rsplit([sep[, maxsplit]]).

How to split the integers and Operators characters from string in python?

I want to split the string into integers and operators for doing Infix expression evaluation in python. Here is my string: I tried this to split: This is wrong. Since ’10’ is splitted into ‘1’,’0′ I tried alternative: This is also went wrong. Since ‘)*’ should be splitted into ‘)’, ‘*’ Could you help to split the operators and integers

How to match a whole word with a regular expression?

I’m having trouble finding the correct regular expression for the scenario below: Lets say: I want to match whole word – for example match “hi” should return False since “hi” is not a word and “is” should return True since there is no alpha character on the left and on the right side. Answer Try From the docs: b Matches

python regex get first part of an email address

I am quite new to python and regex and I was wondering how to extract the first part of an email address upto the domain name. So for example if: I would like the regex result to be (taking into account all “sorts” of email ids i.e including numbers etc..): I get the idea of regex – as in I

Advertisement