I have a little problem with a regex using the "regexp" package in go.
This regex should return to me the substring inside the brackets "[]"
\[(.*?)\] used on #class my-div [button] { should return [ button, hello ]
So, in Go I tried something like:
re := regexp.MustCompile('\[(.*?)\]')
fmt.Println(re.MatchString(header)) // false
return re.FindString(header) // header = "#class my-div [button] {"
And also:
re := regexp.QuoteMeta("\\\[\(\.\*\?\)\\\]") // <= Changed
fmt.Println(re.MatchString(header)) // false
return re.FindString(header) // header = "#class my-div [button] {"
And many other variants, but still doesn't work...
I also tried to use an online regex tester for go, and it works perfectly, so I really don't understand why it doesn't work in go....
http://fiddle.re/57y4c6
Playground: http://play.golang.org/p/Z_-1EEKgaW
Help me please and Thank You for your time!
It's much easier if you just use a raw string literal for regexes, rather than trying to double escape reserved characters. This will compile correctly, and work the same as the fiddle.re example you posted:
re := regexp.MustCompile(`\[(.*?)\]`)
Related
I want to separate that strings (only the strings in English) from this messed string:
"[[[\"Dude, that was insane! \",\"Cara, aquilo foi insano!\",null,null,3],[\"How did you do that? \",\"
I was trying to make a regex using Dart, but it doesn't match:
var regex = RegExp(r'[\"([\w+\s]*)\s\",\"');
Iterable<Match> matches = regex.allMatches(returnString)
matches.forEach((match) {
print(match.group(0));
});
FormatException: Unmatched ')'[\"([\w+.\s]*)\s\",\"
Can someone help me? How can I make a good regex? I'm new at it so sorry about my lack of knowledge.
You can use:
var regex = RegExp(r'"[^"]*"');
which will display:
"Dude, that was insane! "
"Cara, aquilo foi insano!"
"How did you do that? "
Note that your string looks really like a json string and if it is you should use json codec to decode it and recursively go through the tree to collect strings.
I don't have much experience in Go, but basically I want to print my regex on the screen after using it. I cant find anything on Google. This seems something rather easy to do but I have tried several things and nothing else worked.
var swagger_regex = regexp.MustCompile(`[0-9][.][0-9]`)
.... some code here ....
fmt.Println("Your '_.swagger' attribute does not match " + string(swagger_regex))
The regexp.Regexp type has a Regexp.String() method which does this exactly:
String returns the source text used to compile the regular expression.
You don't even have to call it manually, as the fmt package checks and calls the String() method if the type of the passed value has it.
Example:
r := regexp.MustCompile(`[0-9][.][0-9]`)
fmt.Println("Regexp:", r)
// If you need the source text as a string:
s := r.String()
fmt.Println("Regexp:", s)
Output (try it on the Go Playground):
Regexp: [0-9][.][0-9]
Regexp: [0-9][.][0-9]
i have some huge js files and there are some texts/messages/... which are output for a human beeing. the problem is they don't run over the same method.
but i want to find them all to refactor the code.
now i am searching for a regular expression to find those messages.
...his.submit_register = function(){
if(!this.agb_accept.checked) {
out_message("This is a Messge tot the User in English." , "And the Title of the Box. In English as well");
return fals;
}
this.valida...
what i want to find is all the strings which are not source code.
in this case i want as return:
This is a Messge tot the User in
English. And the Title of the Box. In
English as well
i tried something like: /\"(\S+\s{1})+\S\"/, but this wont work ...
thanks for help
It's not possible to parse Javascript source code using regular expressions because Javascript is not a regular language. You can write a regular expression that works most of the time:
/"(.*?)"/
The ? means that the match is not greedy.
Note: this will not correctly handle strings that contain ecaped quotes.
A simple java regex solving your problem (assuming that the message doesn't contain a " character):
Pattern p = Pattern.compile("\"(.+?)\"");
The extraction code :
Matcher m;
for(String line : lines) {
m = p.matcher(line);
while(m.find()) {
System.out.println(m.group(1));
}
}
I am having a terrible time trying to get a regular expression defined to split a string that will look like the following . . .
SQL12345,54321SQL
XXXXX,XXXXX
Where X = [0-9A-Za-z] and can be repeated one or more times on each side of the delimeter (,).
The RegEx Pattern I've come up with is . . .
"([0-9A-Za-z]+)(,)([0-9A-Za-z]+)"
I only ever want one group on each side of the delimeter. I seem to be getting results that look like . . .
myStrArr[0] = ""
myStrArr[1] = "SQL12345"
myStrArr[2] = ","
myStrArr[3] = "54321SQL"
myStrArr[4] = ""
So, why am I getting the line beginning and line end (array elements 0 and 4). And, how can I fix my regex pattern so I don't get these returned?
THANK YOU!
user364939's code won't compile. Try this:
System.String originalString = "SQL12345,54321SQL";
System.String[] splitArray = originalString.Split(',');
System.Console.WriteLine(splitArray[0]);
System.Console.WriteLine(splitArray[1]);
Caveat: I tested this with C# in Snippet Compiler but made the .NET references verbose hoping that it will translate nicely to managed C++.
Here's a managed C++ version:
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
String^ p = "SQL12345,54321SQL";
array<String^>^ a = p->Split(',');
Console::WriteLine(a[0]);
Console::WriteLine(a[1]);
Console::ReadLine();
return 0;
}
Paul's answer could be a better solution if you don't want to use a regular expression. If you do, though, try this one:
([^,]+),([^,]+)
I agree with the dude above. Unless you care about other weird characters like | etc. Then you'd want to split on anything that is not [0-9A-Za-z].
String myString = "SQL12345,54321SQL";
String[] array = myString .split(",");
I've written a url validator for a project I am working on. For my requirements it works great, except when the last part for the url goes longer than 22 characters it breaks. My expression:
/((https?):\/\/)([^\s.]+.)+([^\s.]+)(:\d+\/\S+)/i
It expects input that looks like "http(s)://hostname:port/location".
When I give it the input:
https://demo10:443/111112222233333444445
it works, but if I pass the input
https://demo10:443/1111122222333334444455
it breaks. You can test it out easily at http://ryanswanson.com/regexp/#start. Oddly, I can't reproduce the problem with just the relevant (I would think) part /(:\d+\/\S+)/i. I can have as many characters after the required / and it works great. Any ideas or known bugs?
Edit:
Here is some code for a sample application that demonstrates the problem:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
private function click():void {
var value:String = input.text;
var matches:Array = value.match(/((https?):\/\/)([^\s.]+.)+([^\s.]+)(:\d+\/\S+)/i);
if(matches == null || matches.length < 1 || matches[0] != value) {
area.text = "No Match";
}
else {
area.text = "Match!!!";
}
}
]]>
</mx:Script>
<mx:TextInput x="10" y="10" id="input"/>
<mx:Button x="178" y="10" label="Button" click="click()"/>
<mx:TextArea x="10" y="40" width="233" height="101" id="area"/>
</mx:Application>
I debugged your regular expression on RegexBuddy and apparently it takes millions of steps to find a match. This usually means that something is terribly wrong with the regular expression.
Look at ([^\s.]+.)+([^\s.]+)(:\d+\/\S+).
1- It seems like you're trying to match subdomains too, but it doesn't work as intended since you didn't escape the dot. If you escape it, demo10:443/123 won't match because it'll need at least one dot. Change ([^\s.]+\.)+ to ([^\s.]+\.)* and it'll work.
2- [^\s.]+ is a bad character class, it will match the whole string and start backtracking from there. You can avoid this by using [^\s:.] which will stop at the colon.
This one should work as you want:
https?:\/\/([^\s:.]+\.)*([^\s:.]+):\d+\/\S+
This is a bug, either in Ryan's implementation or within Flex/Flash.
The regular expression syntax used above (less surrounding slashes and flags) matches Python which provides the following output:
# ignore case insensitive flag as it doesn't matter in this case
>>> import re
>>> rx = re.compile('((https?):\/\/)([^\s.]+.)+([^\s.]+)(:\d+\/\S+)')
>>> print rx.match('https://demo10:443/1111122222333334444455').groups()
('https://', 'https', 'demo1', '0', ':443/1111122222333334444455')