How to get the value after a string using perl reg expression - string

I have the following string :
{\"id\":01,\"start_time\":\"1477954800000\",\"stop_time\":\"1485817200000\",\"url\":http:://www.example.com\}
and I'd like to get for example the value of start_time (1477954800000).
I tried several things in https://regex101.com/ but I could not find a way to deal with the special characters (\":\") between the string and the value .
If the for example the string was like start_time = 1477954800000
I know that by using
start_time\":\"(\w+)/)
I'll get the value.
Any idea on how to get the value when \":\" are involved?

Your sample data looks like a stringified JSON object, if that is the case you should use a JSON parser not a regular expression:
#!perl
use strict;
use warnings;
use feature qw(say);
use JSON;
my $json_string = <DATA>;
chomp($json_string);
my $json_object = decode_json $json_string;
# get the value of the start_time key
say $json_object->{start_time};
# 1477954800000
__DATA__
{"id":1,"start_time":"1477954800000","stop_time":"1485817200000","url":"http://www.example.com"}

Related

prepare command and use startswith option with string

I am receiving some rule from file and would like to prepare query at run time but getting following error. any input.
a = "AC"
cond = "startswith"
rule = "AC"
eval("%s.%s(%s)" %(a, cond, rule))
Try in this way:
eval(("'%s'.%s('%s')" %(a, cond, rule)))
You forgot single quotes to define strings in function evaluation
If you must use eval, use %r in your format string when you want to substitute in strings, to get the repr of the str (including quotes) so they remain valid string literals, not raw names; as written, you're trying to run the code AC.startswith(AC), with %r for first and third placeholder (eval("%r.%s(%r)" %(a, cond, rule))) you'd be running "AC".startswith("AC").

How to get demangled function name using regex

I have list of demangled-function names like _Z6__comp7StudentS_
_Z4SortiSt6vectorI7StudentSaIS0_EE. I read wiki and found out that it follows some sort of defined structure. _Z is mangled Symbol followed by a number and then the function name of that length.
So I wanted to retrieve that function name using regex. I only come close to _Z(?:\d)(?<function_name>[a-z_A-Z]){\1}. But referring \1 won't work because its string, right? Is there a single regex pattern solution to this.
You can use 2 capture groups, and get the part of the string using the position of capture group 2
import re
pattern = r"_Z(\d+)([a-z_A-Z]+)"
s = "_Z4SortiSt6vectorI7StudentSaIS0_EE"
m = re.search(pattern, s)
if m:
print(m.group(2)[0: int(m.group(1))])
Output
Sort
Using _Z6__comp7StudentS_ will return __comp

How to remove comma from string 1,398.90 using groovy

I am not able to remove comma from string 1,398.90 using groovy
def liveprice = '1,398.90';
def liveprice2 = liveprice.replaceAll(',', '')
I would really avoid using regular expressions with numbers
Especially numbers that look like money 💰
You can use DecimalFormat to read that String into a BigDecimal (so you keep precision)
import java.text.*
BigDecimal result = DecimalFormat.instance.with {
parseBigDecimal = true
parse('1,398.90')
}
As mentioned by #daggett, your code works fine. Another alternative way besides regex or replace:
'1,39,9,,,,.90'.split(",").join()
// outputs: 1399.90

Python How to split a string when find the first numeric char

This is my string:
"Somestring8/9/0"
I need to get something like this:
['Somestring','8/9/0']
The moment I find a numeric char, I need to split the string to get:
'8/9/0'
This my code:
stringSample = "GigabitEthernet8/9/0"
print re.findall(r'(\w+?)(\d+)', stringSample)[0]
('GigabitEthernet', '8')
But I'm getting this result
What am I doing wrong?
I appreciate your help!!
Your second regex group accepts only digits. Allow it to include forward slashes too.
stringSample = "GigabitEthernet8/9/0"
print re.findall(r'(\w+?)([\d/]+)', stringSample)[0]
# ('GigabitEthernet', '8/9/0')
Try Using the re.split method to split your string in two, passing the maxsplit parameter
re.split('(\w+?)([\d/]+)', stringSample, 1)

How do I print a hash in perl?

How do I print $stopwords? It seems to be a string ($) but when I print it I get: "HASH(0x8B694)" with the memory address changing on each run.
I am using Lingua::StopWords and I simply want to print the stop words that it's using so I know for sure what stop words are there. I would like to print these two a file.
Do I need to deference the $stopwords some how?
Here is the code:
use Lingua::StopWords qw( getStopWords );
open(TEST, ">results_stopwords.txt") or die("Unable to open requested file.");
my $stopwords = getStopWords('en');
print $stopwords;
I've tried:
my #temp = $stopwords;
print "#temp";
But that doesn't work. Help!
Last note: I know there is a list of stop words for Lingua::StopWords, but I am using the (en) and I just want to make absolute sure what stop words I am using, so that is why I want to print it and ideally I want to print it to a file which the file part I should already know how to do.
$ doesn't mean string. It means a scalar, which could be a string, number or reference.
$stopwords is a hash reference. To use it as a hash, you would use %$stopwords.
Use Data::Dumper as a quick way to print the contents of a hash (pass by reference):
use Data::Dumper;
...
print Dumper($stopwords);
to dereference a hashref :
%hash = %{$hashref}; # makes a copy
so to iterate over keys values
while(($key,$value)=each%{$hashref}){
print "$key => $value\n";
}
or (less efficient but didactic purpose)
for $key (keys %{$hashref}){
print "$key => $hashref->{$key}\n";
}
Have a look at Data::Printer as a nice alternative to Data::Dumper. It will give you pretty-printed output as well as information on methods which the object provides (if you're printing an object). So, whenever you don't know what you've got:
use Data::Printer;
p( $some_thing );
You'll be surprised at how handy it is.
getStopWords returns a hashref — a reference to a hash — so you would dereference it by prepending %. And you actually only want its keys, not its values (which are all 1), so you would use the keys function. For example:
print "$_\n" foreach keys %$stopwords;
or
print join(' ', keys %$stopwords), "\n";
You can also skip the temporary variable $stopwords, but then you need to wrap the getStopWords call in curly-brackets {...} so Perl can tell what's going on:
print join(' ', keys %{getStopWords('en')}), "\n";

Resources