regex

Apply Perl RegExp to Remove Parenthesis and Text at End of String

只谈情不闲聊 提交于 2021-02-04 21:38:32
问题 I have a string which includes parenthesis with text inside the parenthesis. How do I remove the parenthesis with text at the end of the string while keeping the other words in string? Input: Potatoes Rice (Meat) Output: Potatoes Rice My code: #! /usr/bin/perl use v5.10.0; use warnings; my $noparenthesis = "Potatoes Rice (Meat)"; $noparenthesis =~ s/^/$1/gi; say $noparenthesis; 回答1: #! /usr/bin/perl use v5.10.0; use warnings; my $noparenthesis = "Potatoes Rice (Meat)"; $noparenthesis =~ s/\(.

Make sure regex does not match empty string - but with a few caveats

感情迁移 提交于 2021-02-04 21:35:24
问题 There is a problem that I need to do, but there are some caveats that make it hard. Problem: Match on all non-empty strings over the alphabet {abc} that contain at most one a . Examples a abc bbca bbcabb Nonexample aa bbaa Caveats: You cannot use a lookahead/lookbehind. What I have is this: ^[bc]*a?[bc]*$ but it matches empty strings. Maybe a hint? Idk anything would help (And if it matters, I'm using python). 回答1: As I understand your question, the only problem is, that your current pattern

Building regex to match 2 words only

为君一笑 提交于 2021-02-04 21:29:27
问题 I'm trying to make regexp that match only 2 words and a single pace between. No special symbols, only [a-zA-Z] space [a-zA-z] . Foo Bar # Match (two words and one space only) Foo # Mismatch (only one word) Foo Bar # Mismatch (2 spaces) Foo Bar Baz # Mismatch (3 words) 回答1: You want ^[a-zA-Z]+\s[a-zA-Z]+$ ^ # Matches the start of the string + # quantifier mean one or more of the previous character class \s # matches whitespace characters $ # Matches the end of the string The anchors ^ and $

Building regex to match 2 words only

北城余情 提交于 2021-02-04 21:28:39
问题 I'm trying to make regexp that match only 2 words and a single pace between. No special symbols, only [a-zA-Z] space [a-zA-z] . Foo Bar # Match (two words and one space only) Foo # Mismatch (only one word) Foo Bar # Mismatch (2 spaces) Foo Bar Baz # Mismatch (3 words) 回答1: You want ^[a-zA-Z]+\s[a-zA-Z]+$ ^ # Matches the start of the string + # quantifier mean one or more of the previous character class \s # matches whitespace characters $ # Matches the end of the string The anchors ^ and $

Escaping regex unicode string in Python

孤者浪人 提交于 2021-02-04 21:23:18
问题 I have a user defined string. I want to use it in regex with small improvement: search by three apostrophes instead of one. For example, APOSTROPHES = re.escape('\'\u2019\u02bc') word = re.escape("п'ять") word = ''.join([s if s not in APOSTROPHES else '[%s]' % APOSTROPHES for s in word]) It works good for latin, but for unicode list comprehension gives the following string: "[\\'\\\\u2019\\\\u02bc]\xd0[\\'\\\\u2019\\\\u02bc]\xbf[\\'\\\\u2019\\\\u02bc][\\'\\\\u2019\\\\u02bc][\\'\\\\u2019\\\

Escaping regex unicode string in Python

守給你的承諾、 提交于 2021-02-04 21:22:15
问题 I have a user defined string. I want to use it in regex with small improvement: search by three apostrophes instead of one. For example, APOSTROPHES = re.escape('\'\u2019\u02bc') word = re.escape("п'ять") word = ''.join([s if s not in APOSTROPHES else '[%s]' % APOSTROPHES for s in word]) It works good for latin, but for unicode list comprehension gives the following string: "[\\'\\\\u2019\\\\u02bc]\xd0[\\'\\\\u2019\\\\u02bc]\xbf[\\'\\\\u2019\\\\u02bc][\\'\\\\u2019\\\\u02bc][\\'\\\\u2019\\\

Escaping regex unicode string in Python

我是研究僧i 提交于 2021-02-04 21:22:02
问题 I have a user defined string. I want to use it in regex with small improvement: search by three apostrophes instead of one. For example, APOSTROPHES = re.escape('\'\u2019\u02bc') word = re.escape("п'ять") word = ''.join([s if s not in APOSTROPHES else '[%s]' % APOSTROPHES for s in word]) It works good for latin, but for unicode list comprehension gives the following string: "[\\'\\\\u2019\\\\u02bc]\xd0[\\'\\\\u2019\\\\u02bc]\xbf[\\'\\\\u2019\\\\u02bc][\\'\\\\u2019\\\\u02bc][\\'\\\\u2019\\\

Regex to match Ipv4 with mask

三世轮回 提交于 2021-02-04 21:17:57
问题 I am validating a Ipv4 address by a regex and it does not support subnet mask. ^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$ Can some one help me with the regex which supports mask as well. Here is a working example of this regex: demo 回答1: Add (?:/[0-2]\d|/3[0-2])? at the end of your regex. You can also simplify the regex: ^([01]?\d\d?|2[0-4]\d|25[0-5])(?:\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])){3}(?:/[0-2]\d|/3[0-2])?$

Regex to match Ipv4 with mask

家住魔仙堡 提交于 2021-02-04 21:17:30
问题 I am validating a Ipv4 address by a regex and it does not support subnet mask. ^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$ Can some one help me with the regex which supports mask as well. Here is a working example of this regex: demo 回答1: Add (?:/[0-2]\d|/3[0-2])? at the end of your regex. You can also simplify the regex: ^([01]?\d\d?|2[0-4]\d|25[0-5])(?:\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])){3}(?:/[0-2]\d|/3[0-2])?$

regex to replace self closing html tags in c#

老子叫甜甜 提交于 2021-02-04 21:07:26
问题 I have an xml which contains some html tags also. When a tag comes in, it breaks the page because it's a self closing tag. Something like: <iframe width="420" height="315" src="//www.youtube.com/embed/6krfYKxJFqA" frameborder="0" /> I want to replace this and convert it to: Can anyone provide a c# code with regex to do this. I tried doing: tmp = tmp.Replace("(<iframe[^>]*)(\\s*/>)", "$1></iframe>"); and tmp = new Regex(@"(<iframe[^>]*)(\\s*/>)").Replace(tmp, "$1></iframe>"); tmp is the xml