w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How do I find a string in text files and print the matches in a Windows batch file?
Not sure which OS platform to be answered for, But Linux/Unix systems can have the following command used for this purpose. find . -name '*.ext' -type f | xargs fgrep 'pattern to be searched' This will search the files starting from current directory and check for the 'pattern to be searched'. The file name pattern with wildcard can be applied where I have used '*.ext'

Categories : Batch File

Multiple matches of regex within other regex
You can use look-around. (?<=one)(bla)*(?=one) Positive look-behind ((?<=...)) checks that the previous characters match some pattern. Positive look-ahead ((?=...)) checks that the next characters match some pattern. Look-around is not included in the matched string. Test. Or you can simply put the part you want to extract in brackets and extract group 1. (the ?: is to make (bla) a non-capturing group, it doesn't change the match, just what gets captured). one((?:bla)*)one Test.

Categories : Regex

Regex that matches anything between ' '
>>> m = re.match("Duplicate entry '(?P<value>.*)' for key '(?P<key>.*)'", "Duplicate entry 'blah@gmail.com' for key 'email'") >>> m.group('value') 'blah@gmail.com' >>> m.group('key') 'email'

Categories : Python

Getting too many matches for regex
"(hello|goodbye). then the reported matches are hello, world and hello*" No, the second "match" is just the result of your capturing group (what's between the parenthesis). Ignore it, or make the group non-capturing: (?:hello|goodbye)

Categories : Javascript

About Regex matches
This regex: ^.*(À|Á|Â|Ã|Ä|Å).*$ says look for: the start of the string (^) followed by a word boundary () followed by any character none or multiple times (.*) followed by a ONE of the uppercase accented A's ((À|Á|Â|Ã|Ä|Å)) followed by a word boundary () followed by any character none or multiple times (.*) followed by the end of the string So it will only match strings that have only one uppercase accented A. The other regex: (À|Á|Â|Ã|Ä|Å) says look for: one of the uppercase accented A's ((À|Á|Â|Ã|Ä|Å)) If you apply the regex globally (usually using the global flag g, some regex functions of some languages apply it by default), then it will execute the regex multiple times. However, if your intention is the remove all accented characters, there

Categories : Regex

Regex matches only part of a URL - why?
To answer your question why the regex doesn't work: It doesn't observe Java's regex syntax rules. Specifically: [^[:punct:]s] doesn't work as you expect it to because Java doesn't recognize POSIX shorthands like [:punct:]. Instead, it treats that as a nested character class. That again leads to the ^ becoming illegal in that context, so Java ignores it, leaving you with a character class that matches the same as [:puncts] which only matches the c of com, therefore ending your match there. As for your question of how to find URLs in a block of text, I suggest you read Jan Goyvaert's excellent blog entry Detecting URLs in a block of text. You'll need to decide yourself how sensitive and how specific you want to make your regex. For example, the solution proposed at the end of the

Categories : Java

regex not giving all the possible matches
Use this regex (?<=([^aeiou]))a(?=([^aeiou])) .net supports group capture in lookarounds..cheers Your code would be var lst= Regex.Matches(input,regex) .Cast<Match>() .Select(x=>x.Groups[1].Value+"a"+x.Groups[2].Value) .ToList(); Now you can iterate over lst foreach(String s in lst) { s;//required strings }

Categories : C#

Regex skipping matches
Change the * quantifier to lazy form (with (.*?) syntax). At the moment it attempts to match as many symbols in the string as possible - and succeeds in doing that, of course, as you have several blocks ending with END. With ? added, the engine will attempt to match the pattern with as few symbols as possible, finishing (.*?) match right before the first END it encounters.

Categories : C#

How to grab all regex matches in php?
You don't need the + after b in your regex. $str = 'brew and this is bunny'; preg_match_all('/b[^s]+/', $str, $matches); print_r($matches); Outputs Array ( [0] => Array ( [0] => brew [1] => bunny ) )

Categories : PHP

Regex that matches only one occurrence
Just use the case where there is a space on both sides. " = " becomes " == " String str = input.replace(" = ", " == "); Edit If you are looking for situations where a=6 is present and want to make it a==6 and you cannot take advantage of targeting the spaces, you may try [w ](=)[w ] Edit live on Debuggex

Categories : Java

Why am I getting multiple regex matches?
Your regex appears to contain a back reference ([[0-9]+])? which would contain square brackets surrounding 1 or more digits, but the ? makes it optional. When applying the regex, the leading and trailing spaces are trimmed by the s+ ... s* The remainder of the string is matched by [a-zA-Z0-9]+s+[a-zA-Z0-9_]+s* And the backreference bit matches the empty string. If you want to match strings that optionally contain that bit, but not return it as a backreference, make it passive with ?: like: ins+[a-zA-Z0-9]+s+[a-zA-Z0-9_]+s*(?:[[0-9]+])?s*

Categories : C++

Regex matches won't separate
Are you trying to find the form with the post method? If so, you're almost there. <form method="post".*>[^*]*</form> http://rubular.com/r/DAi75yjQqU

Categories : HTML

Regex matches incorrect value
string input = "abc {val2:123} lorem ipsum {val1:234}"; var dict = Regex.Matches(input, @"{(.+?):(.+?)}").Cast<Match>() .ToDictionary(m => m.Groups[1].Value, m => m.Groups[2].Value);

Categories : C#

How can I get all matches of a regex in a string?
You can get all the matches like this: my @matches = $page =~ /<h1[^>]*>(.*?)</h1>/ig; print "@matches "; (But note that on yahoo.com, there is a single h1 tag)

Categories : Regex

Get whole line where Regex matches
You can do it like this string[] lines = multilinestring.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); List<string> validString = new List<string>(); foreach(string s in lines) { if(finder.Match(s).Success) { validString.Add(s); } } give this a try as well, should work List<string> lines = multilinestring.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList(); List<string> validString = lines.Where(x => finder.IsMatch(x)).ToList();

Categories : C#

Regex Help to find matches
Depending on the implementation of REGEX: '!<([^>]+)>!' or without delimeters '<([^>]+)>' With look arounds '!(?<=<)[^>]+(?=>)!'

Categories : C#

How to get all possible regex matches in a string
Why do you want strings that could be interpreted as dates but are not valid dates? 2012_13_39 is not a valid date. You could run independent regex for each date type This would look for 4 digit year starting with 19 or 20 The negative look back and look forward is to match on anything not a digit to identify a stand alone number (?<!d)(20|19)dd(?!d) This is to look for month day (?<!d)1?d_[1-3]?d(?!d) but you could be even more restrictive as this allows 19/39 Year at beginning (?<!d)(20|19)dd_1?d_[1-3]?d(?!d) I am not going to build them all up for you but this is the tools to do it (?!d) should work as a boundary

Categories : Dotnet

Regex to find non matches
You can check that with notepad++: search: <a class="web" type="fig(d+)">Fig (?!1)d+</a> And you can do a replaceAll: search: (<a class="web" type="fig)(d+)(">Fig (?!2)(d+)</a>) replace: $1$4$3 Or you can do a blind search/replace, that replaces the attribute with the content in all cases: search: (<a class="web" type="fig)d+(">Fig (d+)) replace: $1$3$2

Categories : Regex

Regex matches but .scan returns nil
That's the way scan works. From the Ruby documentation for scan: If the pattern contains groups, each individual result is itself an array containing one entry per group. Since the optional group ( style=".+") doesn't match you get only a nil in the result. You can use (?: for a non-capturing group: "<p>sdasdasd</p>".scan(/<p(?: style=".+"){0,1}>.+</p>/) # => ["<p>sdasdasd</p>"]

Categories : Ruby

PHP Regex: Test if matches, and then remove
$pattern = '/Approveds?d{2}.d{2}.d{2}/'; I just ran the following code: $html = '<div>This is some text. Approved 11.22.12</div>'; $pattern = '/Approveds?d{2}.d{2}.d{2}/'; if (preg_match($pattern, $html)){ $html = preg_replace($pattern, '', $html); } echo $html; Output: <div>This is some text. </div>

Categories : PHP

Regex.Matches return zero result
No, it doesn't. It works correctly! string match = "Test - Wow"; MatchCollection contact = Regex.Matches(match, "-"); int count = contact.Count; // returns 1 Match onematch = contact[0]; string str = onematch.ToString(); // returns - Test here http://ideone.com/LWTQWn If you want a regex that will return ALL the string if a - is present, something like this .*-.* will do (any number of any character, one -, any number of any character)

Categories : C#

Standard C regex lib matches wrong
Some issues with the code posted (the problems mentioend by the OP might more or less due to those, so fix the issues and see how the code performs): Here int transformRegex(char **regexS){ char tmpStr[strlen(*regexS)+3]; .... *regexS = tmpStr; return 0; } the code returns a reference to memory allocated only for as long being inside transformRegex. It becomes invalid on the function's return. Accessing the memory later on leads to Undefined Behaviour. These lines char *theRegex = (char *) malloc(100); memset(theRegex, 0x00, 100); are meaningless and leaking memory, as in the next line theRegex = values[i]; the value returned by malloc() is overwritten, and therefore lost. The call to free() fails, as the value passed into is referring invalid memory (see ab

Categories : C

RegEx exclude matches between quotes
How about finding: ([^=]+)=("[^"]*"|[^,]+)(?:,s*)? Then extract group 1 (the key) and 2 (the value(s)). [^=]+ - one or more characters that aren't =. "[^"]*" - any characters between quotes. [^,]+ - one or more characters that aren't ,. (?:,s*)? - consume the , with any spaces after it (the only difference between (...) and (?:...) is that the latter doesn't assign a group to it). This won't work if brackets can be nested. Test.

Categories : Regex

RegEx in Python not returning matches
You don't need regex for such simple checks. Better use startswith() and ==: if locus.startswith('Chr3:') and significance == 'yes': UPD: You need to apply strip() on locus and significance variables before the if condition: locus = str(line[3]).strip() significance = str(line[13]).strip()

Categories : Python

regex negative look around with 2 adjacent matches
Your regex simplifies to dog(?!cat) (because the first lookbehind consumes nothing), so it is replacing any instance of dog that is not followed by cat. Try the regex (?!cat).{3}dog(?!cat)

Categories : Javascript

Java Regex to capture several matches
I wouldn't use a regex for this. What about something along the lines of String[] split = s.split("/"); StringBuilder sb = new StringBuilder(s.lastIndexOf('/') + 1); // our result sb.append('/'); // initial "/" for (int i = 0; i < split.length - 1; i++) { // we don't care about the if (split[i].isEmpty()) // last component continue; sb.append(split[i]); sb.append('/'); System.out.println(sb); // or add to an array/list/etc. } /accounts/ /accounts/main/ /accounts/main/index/

Categories : Java

Can I display where a Python regex matches up to?
Your problem is too broad to be handled by Python Regex Engine. You need a finite automata engine (more specific NFA), write your own parser and feed the string and see where it fails and why. Alternatively, you can use PyParsing, create a Grammar (loads of example you will find in the PyParsing Wiki) something like http://pyparsing.wikispaces.com/file/view/dfmparse.py . In case you are new to PyParsing, there would be a steep learning curve, but once you grasp the concept, implementing what you intend would be a cake walk.

Categories : Python

Java regex - overlapping matches
Not sure if this is possible in Java, but in PCRE you could do the following: (?=(d+D+d+)). Explanation The technique is to use a matching group in a lookahead, and then "eat" one character to move forward. (?= : start of positive lookahead ( : start matching group 1 d+ : match a digit one or more times D+ : match a non-digit character one or more times d+ : match a digit one or more times ) : end of group 1 ) : end of lookahead . : match anything, this is to "move forward". Online demo Thanks to Casimir et Hippolyte it really seems to work in Java. You just need to add backslashes and display the first capturing group: (?=(\d+\D+\d+)).. Tested on www.regexplanet.com:

Categories : Java

How can I get the positions of regex matches in ClojureScript?
You can use the .exec method of JS RegExp object. The returned match object contains an index property that corresponds to the index of the match in the string. Currently clojurescript doesn't support constructing regex literals with the g mode flag (see CLJS-150), so you need to use the RegExp constructor. Here is a clojurescript implementation of the re-pos function from the linked page: (defn re-pos [re s] (let [re (js/RegExp. (.-source re) "g")] (loop [res {}] (if-let [m (.exec re s)] (recur (assoc res (.-index m) (first m))) res)))) cljs.user> (re-pos "\w+" "The quick brown fox") {0 "The", 4 "quick", 10 "brown", 16 "fox"} cljs.user> (re-pos "[0-9]+" "3a1b2c1d") {0 "3", 2 "1", 4 "2", 6 "1"}

Categories : Regex

Why is For loop on Regex Matches slow?
In order to find the count, that has to find all the matches in order to count them. Given that you're going to stop after three anyway, that seems a little pointless. Use MatchCollection's lazy evaluation in combination with the Take method from LINQ to only take the first three matches. Usually it's a good idea to use StringBuilder rather than string concatenation in a loop, too: StringBuilder builder = new StringBuilder(...); foreach (var match in matches.Cast<Match>().Take(3)) { builder.AppendFormat("... {0} ...", matches[i].Value); } (The StringBuilder change probably isn't going to make much difference here, but it's a good habit to get into. The Cast method is required because Enumerable.Take only works on the generic IEnumerable<T> type.)

Categories : C#

java get regex overlapping matches
Is this what you are trying to do? String regex = "(?=((Bob|Mary)\b[^\.\?!]*?\b(Paris|London)\b.*?[\.\?!]))"; Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern .matcher("Bob and Mary are planning to go to Paris. They want to leave before July."); while (matcher.find()){ System.out.println(matcher.group(1)); } output: Bob and Mary are planning to go to Paris. Mary are planning to go to Paris. Normally regex will consume what it will match once so it would be impossible to use same part of string in next match. To get rid of this problem we can use look-ahead mechanism (?=...) and groups.

Categories : Java

replacing part of regex matches
To transform get_num(...) to get_num_struct(...), you need to capture the correct text in the input. And, you can't put the parentheses in the regular expression because you may need to match pointers to functions too, as in &get_distance, and uses in comments. However, and this depends partially on the fact that you are using vim and partially on how you need to keep the entire input together, I have checked that this works: %s/get_w+/&_struct/g On every line, find every expression starting with get_ and continuing with at least one letter, number, or underscore, and replace it with the entire matched string followed by _struct. Darn it; I shouldn't answer these things on spec. Note that other regex engines might use & instead of &. This depends on having magic set

Categories : Vim

How to create an array of regex matches
You forgot to add /g modifier, first, and to add + quantifier so the whole domain (and not only its first symbol) would be captured, second. my @domains = $dns_output =~ /(?:[0-9]+.){3}[0-9]+s*([[:graph:]]+)/g; I'd compressed IP grouping as well here, and have added * quantifier to s separating the domain.

Categories : Regex

Modifying regex matches in replacements
you can use a callback function with gsub, example: "some_thing_good".gsub(/_([a-z])/) {|m| m[1].upcase} To avoid strings inside double-quotes you can do that: ""look_at_me" some_thing_good".gsub(/"[^"]+"|_[a-z]/) {|m| (m.length>2)? m : m[1].upcase } The idea is to match them before and replace them by themselves. If i test the match length, i know immediatly which part of the alternation has been matched, since the second part contains only 2 characters and the first part at least 3 characters.

Categories : Ruby

even matches lost using match on a regex
Try changing the regex as /(*[a-z0-9]{3,150})/img See it in action: http://regexr.com?35ae3 EDIT: Following @AndyE suggestion, the regex could be written as /(?:s+|^)(*[a-z0-9]{3,150})/img, http://regexr.com?35aec

Categories : Javascript

How do go about Regex.Matches group in visual C#
Parenthesis will create the 2 groups you need. Also, you will have to escape the following characters: $,[,],? and you can do this with "". So, you're regex will look something like: "< php $video[0]='(.*)';$title[0]='(.*)'; ?>"

Categories : C#

Stepping through regex pattern matches in Python:
Loop over the token and patterns in pairs, so you can adjust the following: for pat, token in zip([pat1, pat2, pat3], ['tok1', 'tok2', 'tok3']): m = pat.match(buffer) if m: val = m.group(0) tok = token1 break The idea is you build up a table before hand of pattern -> values: tests = [ (re.compile('([a-z]{2})'), 'func1'), (re.compile('(a{5}'), 'token2') ] for pattern, token in tests: m = pattern.match(buffer) if m: # whatever This could further be extended to provide a callable instead which could take the compiled object and buffer as arguments and do whatever it wants from that and returns a value. eg: def func1(match, buf): print 'entered function' return int(buf) * 50 tests = [ (re.compile('d+'), func1) ] f

Categories : Python

Revisited: zero-length matches in Java regex
a? means zero or one 'a' characters, so it will match an a or a "nothing" - it's matching the "a" chars and "nothingness" positioned between the "a" and "b" characters. This is completely correct and expected.

Categories : Java

prevent regex matches both ahead and behind with lookarounds?
Without look-behinds, you can use this regex to match anything that has music but has no stop/kill: (?!.*(stop|kill)).*music.* Results: "stop music" -> matches() == false "music stop" -> matches() == false "music go" -> matches() == true "go music" -> matches() == true "music go stop" -> matches() == false Sampling code used: String[] words = {"stop music", "music stop", "music go", "go music", "music go stop" }; for (String word : words) { System.out.println("""+word+"" -> matches() == "+(word.matches("(?!.*(stop|kill)).*music.*"))); }

Categories : Java

Multiline Regex matches first occurance but can't match second
Regex is not really the right solution for this, but if you must... Your problem is a combination of regex greediness and starting the match with ^. If it starts with ^ it needs it to start the string and it won't match anywhere else. The greediness of .* can be fixed by making it .*? instead. Try this: (d{4}-d{2}-d{2} d{2}:d{2}:d{2}) - (.*?) (Work notes) ([wW]*?)((?= d{4}-d{2}-d{2} d{2}:d{2}:d{2} - .*? (Work notes) )|((s{0,})$))

Categories : C#



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.