Replace characters in string like in python - crystal-lang

In python there's function replace(old, new) which replaces "old" to "new" in some string, is there any function like this or any way to do it in Crystal?

From the Crystal-lang api docs you can use the .sub function:
"hello yellow".sub("ll", "dd") # => "heddo yellow"
Source:
https://crystal-lang.org/api/0.23.1/String.html
NOTE: This function only replaces the first occurrence of the search string. There also appears to be a version of the sub function that allows you to pass a regex string which should allow you to grab all occurrences of a particular string.

Related

Add a word before each extracted String

I am capturing the session id from the string. I want to add word(prefix) before the extracted session id.
Sample input: key=this is sample input; MySessionId=hhjsfd436763jhjhfdjs87787.hghht77f54; key7=jhu8787; type=raw; oldkey=jkjf8787;
I have formed the below regex to capture the MySessionId.
MySessionId=([^.]*)
I want to add a word before the extracted string like below.
Expected output:
ABCD-1234-hhjsfd436763jhjhfdjs87787
Any way to achieve this through Regular expression?
It really depends what language you're using, you'll need to find a function that replaces text in a string (usually it's called replace). It looks like you're dealing with cookies so I'll show you an example in javascript:
//$1 refers to the first group captured by the regex
//i think other languages use $1 too but you should probably check
string = string.replace(/MySessionId([^.]*)/, "ABCD-1234-$1")

Javascript Storing Regexp screws the original format

I have an angular app where a user can add a regexp in a form, a value like:
github\.com/([A-Za-z0-9\-\_]+)/([A-Za-z0-9\-\_]+)
When I store this in the localStorage and I inspect the localStorage:
github\\\\.com\\/([A-Za-z0-9\\\\-\\\\_]+)\\/([A-Za-z0-9\\\\-\\\\_]+)
When I retrieve in Javascript elsewhere this value I get:
github\\.com\/([A-Za-z0-9\\-\\_]+)\/([A-Za-z0-9\\-\\_]+)
This is not the original regexp and the match method in Javascript can't work.
NOTE: after submitting the form, I store the object with:
localStorage.myobject = JSON.stringify(myobject);
You can get rid of overescaping here, just use
github[.]com/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)
and initialize it via a RegExp constructor so as not to have to escape the regex delimiter /. A dot inside [] loses its special meaning and only matches a literal dot, the hyphen at the end of the character class only matches a literal hyphen, and the _ does not have to be escaped at all anywhere in the pattern:
var tst = "github.com/Test1-Text/Test2";
var pattern = "github[.]com/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)";
console.log(new RegExp(pattern).test(tst));
UPDATE:
When using patterns from external sources, you need to use the constructor notation. Make sure your regex patterns are stored as literal strings (if you had RegExp("C:\\\\Folder"), make sure it is stored as C:\\Folder), and when reading the value in it will be automatically be usable with the RegExp constructor.

Python RegEX that gets each name of a function in a file

Trying to implement a regex script that gets the name of each function and returns them to a text file. The returning to text file part I got, the part I need some pointers in I don't.
# I just want to extract "name_i_want"
def name_i_want(self):
A regex for this could be:
(?<=def )(\w+)(?=\()
Working regex example:
http://regex101.com/r/qR3fE7

Regex Assistance for a url filepath

Can someone assist in creating a Regex for the following situation:
I have about 2000 records for which I need to do a search/repleace where I need to make a replacement for a known item in each record that looks like this:
<li>View Product Information</li>
The FILEPATH and FILE are variable, but the surrounding HTML is always the same. Can someone assist with what kind of Regex I would substitute for the "FILEPATH/FILE" part of the search?
you may match the constant part and use grouping to put it back
(<li>View Product Information</li>)
then you should replace the string with $1your_replacement$2, where $1 is the first matching group and $2 the second (if using python for instance you should call Match.group(1) and Match.group(2))
You would have to escape \ chars if you're using Java instead.

Article spinner with 2 tiers

I made an article spinner that used regex to find words in this syntax:
{word1|word2}
And then split them up at the "|", but I need a way to make it support tier 2 brackets, such as:
{{word1|word2}|{word3|word4}}
What my code does when presented with such a line, is take "{{word1|word2}" and "{word3|word4}", and this is not as intended.
What I want is when presented with such a line, my code breaks it up as "{word1|word2}|{word3|word4}", so that I can use this with the original function and break it into the actual words.
I am using c#.
Here is the pseudo code of how it might look like:
Check string for regex match to "{{word1|word2}|{word3|word4}}" pattern
If found, store each one as "{word1|word2}|{word3|word4}" in MatchCollection (mc1)
Split the word at the "|" but not the one inside the brackets, and select a random one (aka, "{word1|word2}" or "{word3|word4}")
Store the new results aka "{word1|word2}" and "{word3|word4}" in a new MatchCollection (mc2)
Now search the string again, this time looking for "{word1|word2}" only and ignore the double "{{" "}}"
Store these in mc2.
I can not split these up normally
Here is the regex I use to search for "{word1|word2}":
Regex regexObj = new Regex(#"\{.*?\}", RegexOptions.Singleline);
MatchCollection m = regexObj.Matches(originalText); //How I store them
Hopefully someone can help, thanks!
Edit: I solved this using a recursive method. I was building an article spinner btw.
That is not parsable using a regular expression, instead you have to use a recursive descent parser. Map it to JSON by replacing:
{ with [
| with ,
wordX with "wordX" (regex \w+)
Then your input
{{word1|word2}|{word3|word4}}
becomes valid JSON
[["word1","word2"],["word3","word4"]]
and will map directly to PHP arrays when you call json_decode.
In C#, the same should be possible with JavaScriptSerializer.
I'm really not completely sure WHAT you're asking for, but I'll give it a go:
If you want to get {word1|word2}|{word3|word4} out of any occurrence of {{word1|word2}|{word3|word4}} but not {word1|word2} or {word3|word4}, then use this:
#"\{(\{[^}]*\}\|\{[^}]*\})\}"
...which will match {{word1|word2}|{word3|word4}}, but with {word1|word2}|{word3|word4} in the first matching group.
I'm not sure if this will be helpful or even if it's along the right track, but I'll try to check back every once in a while for more questions or clarifications.
s = "{Spinning|Re-writing|Rotating|Content spinning|Rewriting|SEO Content Machine} is {fun|enjoyable|entertaining|exciting|enjoyment}! try it {for yourself|on your own|yourself|by yourself|for you} and {see how|observe how|observe} it {works|functions|operates|performs|is effective}."
print spin(s)
If you want to use the [square|brackets|syntax] use this line in the process function:
'/[(((?>[^[]]+)|(?R))*)]/x',