regex

How do I write a regular expression that excludes rather than matches, e.g., not (this|string)?

I am stumped trying to create an Emacs regular-expression that excludes groups. [^] excludes individual characters in a set, but I want to exclude specific sequences of characters: something like [^(not|this)], so that strings containing "not" or "this" are not matched. In principle, I could write ([^n][^o][^t]|[^...]), but is there ano...

With sed or awk, how do I match from the end of the current line back to a specified character?

I have a list of file locations in a text file. For example: /var/lib/mlocate /var/lib/dpkg/info/mlocate.conffiles /var/lib/dpkg/info/mlocate.list /var/lib/dpkg/info/mlocate.md5sums /var/lib/dpkg/info/mlocate.postinst /var/lib/dpkg/info/mlocate.postrm /var/lib/dpkg/info/mlocate.prerm What I want to do is use sed or awk to read f...

Getting last occurrence of a string using regular expressions

Hi everone! I need to parse an HTML file and i've got something like this: <TAG1> <TAG1> TEXT_TO_FIND KEY <TAG1> </TAG1> <TAG1> </TAG1> </TAG1> </TAG1> Taking into account that there are multiple levels of anidation. How can I get the text TEXT_TO_FIND? In plain english, what I ...

Parse text using regular expressions

I have a dictionary in .txt format, which looks like this: term 1 definition 1 definition 2 term 2 definition 1 definition 2 definition 3 etc. There is a tab always before a definition, basically it's like this: term 1 [tab]definition 1 [tab]definition 2 etc. Now I need to wrap every term and it's definitions w...

Match domain name from url (www.google.com=google)

So I want to match just the domain from ether: http://www.google.com/test/ http://google.com/test/ http://google.net/test/ Output should be for all 3: google I got this code working for just .com echo "http://www.google.com/test/" | sed -n "s/.*www\.\(.*\)\.com.*$/\1/p" Output: 'google' Then I thought it would be as simple as doin...

Regular expression to find two strings anywhere in input

How do I write a regular expression to match two given strings, at any position in the string? For example, if I am searching for cat and mat, it should match: The cat slept on the mat in front of the fire. At 5:00 pm, I found the cat scratching the wool off the mat. etc. No matter what precedes these strings. ...

How to create a regex for following condition?

Hi, I was creating a regex for following condition a string can contain any alphabet, digit and ' and ? the string should start with either alphabet or digit for ex: adsfj asfj's jfkd'sdf? df ds? afjdk? are all valid I use C# 2.0 I tried something like this ^[a-zA-Z0-9]+[']\*[a-zA-Z0-9]\*[?]\*[a-zA-Z0-9]\*$ which did not s...

Reg exp for youtube link

In a system that I'm developing I need to recognize the youtube link in the following format [youtube]youtube url[/youtube] for the moment I arrived at this regular expression: #\[youtube\]http://www.youtube\.(.*)/watch\?v=([a-zA-Z0-9_-]*)\[\/youtube\]#s But this pattern isn't able to recognize url like [youtube]http://www.y...

regex for Apache logs in PHP

Hy im trying to split this string in PHP. 11.11.11.11 - - [25/Jan/2000:14:00:01 +0100] "GET /1986.js HTTP/1.1" 200 932 "http://domain.com/index.html" "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6" How can split this in to ip,date,http method domainname and Browser ? ...

Regular expression to match a block of text up to the first double new line?

I'm making a simple Textile parser and am trying to write a regular expression for "blockquote" but am having difficulty matching multiple new lines. Example: bq. first line of quote second line of quote third line of quote not part of the quote It will be replaced with blockquote tags via preg_replace() so basically it needs to mat...

File name matching with -e and regular expressions in perl.

I need to check for the existence of a file in a directory. The file name has a pattern like: /d1/d2/d3/abcd_12345_67890.dat In my program, I will know the file name up to abcd_ I need to write an if condition using -e option and find the files matching above given pattern. ...

Need a regular expression to trim a css file of all values, just keep selectors

Used to have this but lost it. Could someone assist? Its a short reg expression that I pasted into TextMates search replace to trim a css file in this way. It finds all text between {} and removes it. selector { value: blah; } Becomes.. selector {} Its so i can clean a css file out ready for theming from scratch. Thanks ...

Regular expression to replace this string

I have this string "ABC-2341241244 | tb1 | value | tb2 | value | tb10 | value" How can I do with regex to replace for example '| tb2 | value ' with '' to stay with this, "ABC-2341241244 | tb1 | value | tb10 | value" I know the value 'tbxx' is a varieble that I have. The Regex engine is javascript sorry not .Net C# sorry ...

regular expressions with php

I am trying to write a regular expression to capture this string: <td style="white-space:nowrap;">###.##</td> I can't even match it if include the string as it is in the regex pattern! I am using preg_match_all(), however, I am not finding the correct pattern. I am thinking that "white-space:nowrap;" is throwing off the matching in so...

Building a "Semi-Natural Language" DSL in Ruby

I'm interested in building a DSL in Ruby for use in parsing microblog updates. Specifically, I thought that I could translate text into a Ruby string in the same way as the Rails gem allows "4.days.ago". I already have regex code that will translate the text @USER_A: give X points to @USER_B for accomplishing some task @USER_B: take Y p...

Using regular expressions with C++ on Unix

Hello, I'm familiar with Regex itself, but whenever I try to find any examples or documentation to use regex with Unix computers, I just get tutorials on how to write regex or how to use the .NET specific libraries available for Windows. I've been searching for a while and I can't find any good tutorials on C++ regex on Unix machines. ...

How do I check if a string has at least one number in it using Ruby?

I need to check to see if a string contains at least one number in it using Ruby (and I assume some sort of regex?). How would I do that? ...

translate by replacing words inside existing text

What are common approaches for translating certain words (or expressions) inside a given text, when the text must be reconstructed (with punctuations and everythin.) ? The translation comes from a lookup table, and covers words, collocations, and emoticons like L33t, CUL8R, :-), etc. Simple string search-and-replace is not enough since...

Is_prime function via regex in python (from perl)

I've read this article where the /^1?$|^(11+?)\1+$/ Perl regex is used to test if a number is prime or not. Process: s = '1' * your_number If s matchs the regex, then it's not prime. If it doesn't, it's prime. How would you translate that regex to Python's re module? ...

Java regex; conditions on groups

I want to create a regex, where the occurence of one group, depends on whether or not another certain group has been found. I think this is easier to illustrate with an example! I want to allow two patterns, illustrated by these two examples: JsJh, JJ. This is not allowed: JsJs, JsJ, JQ. So if the user types one small letter after the ...