Posts

Showing posts with the label regex

Perl compatible regular expression (PCRE) in Python

Perl compatible regular expression (PCRE) in Python I have to parse some strings based on PCRE in Python, and I've no idea how to do that. Strings I want to parse looks like: match mysql m/^.n(4.[-.w]+).../s p/MySQL/ i/$1/ In this example, I have to get this different items: "m/^.n(4.[-.w]+).../s" ; "p/MySQL/" ; "i/$1/" The only thing I've found relating to PCRE manipulation in Python is this module: http://pydoc.org/2.2.3/pcre.html (but it's written it's a .so file ...) Do you know if some Python module exists to parse this kind of string? Just to be clear: You want to match PCREs, not use them? – Tim Pietzcker Aug 15 '11 at 9:37 I have to use them – Steeve Aug 15 '11 at 9:41 ...

Regex regular expression minimum character amount

Regex regular expression minimum character amount I have the following regular expression: ^[8][0][1-3][0-9][0-9][0-9] How would I make it so that I have this expression but also so that the minimum amount of characters is 6. Thanks Your pattern already matches 6 numbers. What is your question? – Tim Biegeleisen Jun 29 at 9:47 It exactly does what you ask for. It matches 6 digits at the start of the string. – Wiktor Stribiżew Jun 29 at 9:47 1 Answer 1 Your pattern already matches what you asked for. If you want to simplify it you can write it as: ^80[1-3][0-9]{3} And if you want at least 6 characters but rem...

Split on every character inside square brackets

Split on every character inside square brackets I have a problem with a regex, I have the following string [date|Y-m-d] which is a Token extracted from a text and I need to extract 'date' and 'Y-m-d', but I haven't been able to achieve my goal with the following regex, can someone help me (?:[)([^|]+)(?:]) The tokens can be like: [date|Y-m-d] should extract 'date' and 'Y-m-d' [date|Y-m-d|today] should extract 'date', 'Y-m-d' and 'today' Use: $tok = preg_split('/[|]/', $str, -1, PREG_SPLIT_NO_EMPTY) and then use $tok[0] and $tok[1] – anubhava Jun 29 at 9:21 $tok = preg_split('/[|]/', $str, -1, PREG_SPLIT_NO_EMPTY) $tok[0] $tok[1] @claudio See this demo. It is for scenarios where only 1 match per string is expected. ...

Regex in Java matching specific special characters with number

Regex in Java matching specific special characters with number I would like to have a regex which accepts numeric along with specific special characters ( .-, ). I just learned about basics of regex and I don't know how come my pattern doesn't work, really need advice. .-, My Pattern ^(([0-9]*)+[.,-]{0,1})$ ( .,- ) can only be repeated once, that's {0,1} . Also first should be numeric and last would also be numeric. I really need a little push. .,- {0,1} Expected output 122-555-1521 //true 155--122 //false 155,- //false .-12 //false 123.123. //false .12 //false 1.2,1-3 //true can you show us some expected outputs? – YCF_L Jun 29 at 9:10 You can use: ^d+(?:[.,-]d+)*$ – anubhava Jun 29 at 9:15 ...

Remove all -es/-s/-e/-x suffixes that follow 4 or more characters

Remove all -es/-s/-e/-x suffixes that follow 4 or more characters I am trying to delete all word suffixes -es , -s , -e or -x of all words that have at least 4 characters after removing the suffix, using regex in Python. -es -s -e -x There are some examples of desired output (in French) : I tried to implement as shown below, but I do not find it very efficient. def _stem_reg(word): pattern = "(w{4,})(es$)|(w{4,})(s$)|(w{4,})(e$)|(w{4,})(x$)" found = re.match(pattern, word) if found is not None: return next(group for group in found.groups() if group is not None) else: return word What exactly do you mean by "not very efficient"? – Thierry Lathuille Jun 27 at 16:48 Try re.sub(r'b(w{4,})(?:e?s|[ex])b', r'1', s) – Wiktor Stribiż...

Regex ignore multiple wrong placed quotes

Regex ignore multiple wrong placed quotes From this input: """ "01-01-2000""" " ",""" "Bank123""" "", "" ""Example text" " "", I want to extract: 01-01-2000 Bank123 Example text I managed this: (["'])(?:(?=(?))2.)*?1 But if fails if it comes to deal with many wrong placed quotes. Any ideas? Try "b(.*?)b" – Wiktor Stribiżew Jun 29 at 8:41 "b(.*?)b" Or, B"b(.*?)b"B . Can the values you want to extract contain " ? Why did you use a regex that matches escaped " ? – Wiktor Stribiżew Jun 29 at 8:48 B...

Regex, selecting from second regex expression if only it is not in first regex expression

Regex, selecting from second regex expression if only it is not in first regex expression For example- I have this text www.google.com www.google.com <a href="www.google.com"> Google Homepage </a> <a href="www.google.com"> Google Homepage </a> I wrote this (<a.*</a>) which captures anchor tag and this (www.[S]+(b|$)) which selects any text which starts with www. but what i want it selects only www.google.com not the one inside anchor tag. (<a.*</a>) (www.[S]+(b|$)) www. www.google.com anything through which I can completely ignore anchor tag and select text only from remaining text. To be more precise a regex which can: NOT OF (<a.*</a>) AND (www.[S]+(b|$)) (<a.*</a>) (www.[S]+(b|$)) Hope, I'm clear with my question. Thanks for helping. Do not try to parse html with regex – Ulysse BN Jun 29 at 7:29 ...

write a regex for compile runtime keyword

write a regex for compile runtime keyword text = """ Pratap pandey age 25 student """ keyword = "age" re_compile = re.compile('((.*n+){2})keyword((.*n+){2})') re_result = re.findall(re_compile, text) I want to write a regex for extracting two lines before keyword and two lines after keyword when keyword is matched, with variable. Would you please add an example? – andrew Jun 28 at 9:34 If i try with re_compile = re.compile('((.*n+){2})age((.*n+){2})'), then works, age instead of keyword, but i want to extract with variable name – Pratap Jun 28 at 9:35 I mean, an example of input and expe...

listing all ipv4 addresses from LOG using PCRE regex

listing all ipv4 addresses from LOG using PCRE regex I need to write a regular expression which will list all ip addresses from logs. The addresses should have different groups, so it should not match only to full match. The amount of ip addresses is not constant. The log looks like this <177>Jun 28 15:35:15 src=192.168.100.122 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Null scan (attempts with response: 144, attempts without response: 0, targets: 56, port(s): 443, 80, 5274, 445, 88, 4120, 135, 49155). targetList: 12.97.135.114, 18.232.35.16, 10.10.202.7, 10.101.90.178, 30.101.124.18, 13.107.3.128, 10.83.127.51, 12.160.91.170, 10.101.124.34, 10.10.200.12 <177>Jun 28 15:35:15 src=192.168.100.122 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Null scan (attempts with response: 144, attempts without response: 0, targets: 56, port(s): 443, 80, 5274, 445, 88, 4120, 135, 49155). targetList: 12.97.135.114, 18.232.35.16, 10.10.202.7, 10...

Regular Expressions to restrict specific user input

Regular Expressions to restrict specific user input I want to restrict users to enter data in the form Hello World ... I want to use regular expressions but I am not good at it. My attempt was: ^([A-Z]{1})([a-z]{1,})([ ])?([A-Z]{1})?([a-z]{1,})?$ ^([A-Z]{1})([a-z]{1,})([ ])?([A-Z]{1})?([a-z]{1,})?$ For the groups starting at ([ ])? Are supposed to be optional... For instance, the following are valid entries: Hello , Hello World , People , People Talk The following are invalid: people , people Talk , People talk , people talk , heLlo This REGEX would have to be case-sensative as the previous examples imply. REGEX Consider this tool – Michele Lambertucci 2 days ago Does this do what you want? ^([A-Z]{1})([a-z]{1,})s*(([A-Z]{1})+([a-z]{1,}))?$ – Michele Lambertucci 2 days ago ...