Regex for matching and separating strings - regex

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.

Related

get number after matching string in ruby with regex

I'm trying to get the number of tracking_number from the string.
string = '{:rate_type=>"PAYOR_ACCOUNT_PACKAGE", :rated_weight_method=>"ACTUAL", tracking_number=>"795856589804"}, :group_number=>"0", :package_rating=>{:actual_rate_type=>"PAYOR_ACCOUNT_PACKAGE", :package_rate_details=>{:rate_type=>"PAYOR_ACCOUNT_PACKAGE", :rated_weight_method=>"ACTUAL", :minimum_charge_type=>"CUSTOMER_FREIGHT_WEIGHT", :billing_weight=>{:units=>"LB", :value=>"1.0"}}'
I have tried /tracking_number=>['"]((.*?)['"])*/ but getting all the string after the match.
Can anybody help me on this.
I have tried this at https://rubular.com/r/ZcmJinTHDQSDsZ
Output I want is 795856589804
Remove * from end of your regex. This is the reason you are getting all the string after match.
And If you want to get just the number part then use this regex.
/tracking_number=>"(\d+)"/

Regex without brackets

I have the following tag from an XML file:
<msg><![CDATA[Method=GET URL=http://test.de:80/cn?OP=gtm&Reset=1(Clat=[400441379], Clon=[-1335259914], Decoding_Feat=[], Dlat=[0], Dlon=[0], Accept-Encoding=gzip, Accept=*/*) Result(Content-Encoding=[gzip], Content-Length=[7363], ntCoent-Length=[15783], Content-Type=[text/xml; charset=utf-8]) Status=200 Times=TISP:270/CSI:-/Me:1/Total:271]]>
Now I try to get from this message: Clon, Dlat, Dlon and Clat.
However, I already created the following regex:
(?<=Clat=)[\[\(\d+\)\n\n][^)n]+]
But the problem is here, I would like to get only the numbers without the brackets. I tried some other expressions.
Do you maybe know, how I can expand this expression, in order to get only the values without the brackets?
Thank you very much in advance.
Best regards
The regex
(clon|dlat|dlon|clat)=\[(-?\d+)\]
Gives
As I stated before, if you use this regex to extract the information out of this CDATA element, that's okay. But you really want to get to the contents of that element using an XML parser.
Example usage
Regex r = new Regex(#"(clon|dlat|dlon|clat)=\[(-?\d+)\]");
string s = ".. here's your cdata content .. ";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
{
var name = match.Groups[1].Value; //will contain "clon", "dlat", "dlon" or "clat"
var inner_value = match.Groups[2].Value; //will contin the value inside the square-brackets, e.g. "400441379"
//Do something with the matches
}

Go-lang Error using regex

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(`\[(.*?)\]`)

Capitalize first letter of words in a string

I'm having trouble figuring out how to transform a string into camel case in groovy. Say I start out with a string that looks like "1-800 FOO.BAR". Ultimately, I want this to turn into "1800FooDotBar". I've been able to get 1800FOODotBar by doing the following:
String str = "1-800 FOO.BAR"
String tempStr = str.replaceAll(/(?i)\.com/, "DotCom")
String newStr = tempStr.replaceAll(/\\W/, "")
I'm just not sure how to get rid of those capital letters in the middle. I've come across some information about a capitalize() method that should be able to help, but I'm just not familiar enough with Groovy to know how to use it. I think I need to split the string into individual strings for each word and then capitalize the first letter of each of those strings, but then how do I build the end result back up? I know that similar questions have been asked, but I'm just not seeing how to take that information and make complete Groovy code from it. Thanks in advance!
Very roughly:
String str = "1-800 FOO.BAR"
println str.replaceAll(/\./, " Dot ").split(/[^\w]/).collect { it.toLowerCase().capitalize() }.join("")
=> 1800FooDotBar

looking for a regular expression to extract all text outputs to user from js file

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));
}
}