regex for parsing string in matlab - regex

Just a small question. I need to parse
xyz1/Allrun1Mbps_10000us.sca
and extract values between "_" and "us" (here for example 10000). I am not able to create the regex properly for it.

According to this, matlab supports lookarounds. (?<=_).*?(?=us) uses lookarounds to check the information before or after is present. _ and us respectivelly. You can rename the _ and us depending on your needs.
You could also use the site I mentioned to craft your own regular expressions from now on. The first result of google when you write "matlab regex" has all the answers you need.

you can use this _(.*?)us. $1 gives the result.
or in more specific _(\d+?)us

Related

What is the proper way to check if a string contains a set of words in regex?

I have a string, let's say, jkdfkskjak some random string containing a desired word
I want to check if the given string has a word from a set of words, say {word1, word2, word3} in latex.
I can easily do it in Java, but I want to achieve it using regex. I am very new to regular expressions.
if you want only to recognise the words as part of a word, then use:
(word1|word2|...|wordn)
(see first demo)
if you want them to appear as isolated words, then
\b(word1|word2|...|wordn)\b
should be the answer (see second demo)
I am not able to understand the complete context like what kind of text you have or what kind of words will this be but I can offer you a easy solution the literal way programmatically you can generate this regex (dormammu|bargain) and then search this in text like this "dormammu I come to bargain". I have no clue about latex but I think that is not your question.
For more information you can tinker with it at [regex101][1].
If you are having trouble understanding it [regexone][2] this is the place to go. For beginners its a good start.
[1]: http://regex101.com [2]: https://regexone.com/

online tool available to validate regex in firestore?

There are tools available to validate the regex used in javascript / prolong etc but i am writing rules in google-cloud-firestore. I want some tool to check my regex.
please suggest.
If you read my original answer. Ignore it.
You can use the matches comparison.
matches
Performs a regular expression match, returns true if the whole
string matches the given regular expression. Uses Google RE2 syntax.
The full list of string validation rules available for Cloud Firestore are shown here.

Is there a function to create a regex pattern from a string input?

I'm lousy at regular expressions but occasionally they're the only thing that's the right solution for a problem.
Is there something in the .NET framework that allows you to input an unencoded string and get a pattern from it? Which you could then modify as required?
e.g. I want to remove a CDATA section that contains a file from some XML but I can't work out what the right pattern is for <![CDATA[hugepileofrandombinarydataherethatalsoneedstogo]]> and I don't want to ask for help each time I'm stuck on a regex pattern.
Such tools exist, google by "regex generator".
But, as suggested in comments, better learn regex. Simple patterns are easy. Something like <!\[.*?]]>
in your case.
There are Regex Design tools like expresso...
http://www.ultrapico.com/expresso.htm
It's not perfect but as there is no suitable .Net component the text to regex page at txt2re.com is the best I've seen for those people who occasionally need to build a regex to match a string but don't have the time to relearn regex each time they want to use one.

Do calculation on captured number in regex before using it in replacement

Using a regex, I am able to find a bunch of numbers that I want to replace. However, I want to replace the number with another number that is calculated using the original - captured - number.
Is that possible in notepad++ using a kind of expression in the replacement-part?
Edit: Maybe a strange thought, but could the calculation be done in the search part, generating a second captured number that would effectively be the result?
Even if it is possible, it will almost certainly be "messy" - why not do the replacements with a simple script instead? For example..
#!/usr/bin/env ruby
f = File.new("f1.txt", File::RDWR)
contents = f.read()
contents.gsub!(/\d+/){|m|
m.to_i + 1 # convert the current match to an integer, and add one
}
f.truncate(0) # empty the existing file
f.seek(0) # seek to the start of the file, before writing again
f.write(contents) # write modified file
f.close()
..and the output:
$ cat f1.txt
This was one: 1
This two two: 2
$ ruby replacer.rb
$ cat f1.txt
This was one: 2
This two two: 3
In reply to jeroen's comment,
I was actually interested if the possibility existed in the regular expression itself as they are so widespread
A regular expression is really just a simple pattern matching syntax. To do anything more advanced than search/replace with the matches would be up to the text-editors, but the usefulness of this is very limited, and can be achieved via scripting most editors allow (Notepad++ has a plugin system, although I've no idea how easy it is to use).
Basically, if regex/search-and-replace will not achieve what you want, I would say either use your editors scripting ability or use an external script.
Is that possible in notepad++ using a kind of expression in the replacement-part?
Interpolated evaluation of regular-expression matches is a relatively advanced feature that I probably would not expect to find in a general-purpose text editing application. I played around with Notepad++ a bit but was unable to get this to work, nor could I find anything in the documentation that suggests this is possible.
Hmmm... I'd have to recommend AWK to do this.
http://en.wikipedia.org/wiki/AWK
notepad++ has limited regular expressions built in. There are extensions that add a bit more to the regular expression find and replace, but I've found those hard to use. I would recommend writing a little external program to do it for you. Either Ruby, Perl or Python would be great for it. If you know those languages. I use Ruby and have had lots of success with it.

Algorithm to get a Regex

Something like this is on my mind: I put one or a few strings in, and the algorithm shows me a matching regex.
Is there an "easy" way to do this, or does something like this already exist?
Edit 1: Yes, I'm trying to find a way to generate regex.
Edit 2: Regulazy is not what I am looking for. The common use for the code I want is to find a correct RegEx; for example, article numbers:
I put in 123456, the regex should be \d{6}
I put in nb-123456, the regex should be \w{2}-\d{6}
If you have Emacs you can use regexp-opt. For example, evaluating:
(regexp-opt (list "my" "list" "of" "some" "strings" "to" "search"))
returns
"list\\|my\\|of\\|s\\(?:earch\\|ome\\|trings\\)\\|to"
Perl can do it: http://www.hakank.org/makeregex/
So does ruby: http://www.toolbox-mag.de/data/makeregex.html
Note: not so perfect solution.
And there is a CLI tool: txt2regex.
There was txt2re, once upon a time...
It sounds like you want an algorithm to generate a regular grammar based on some samples. In a lot of cases, there are many possible grammars for a given set of examples--there can even be infinite possible grammars. Of course, the possibilities can be limited by a second set of required non-matches, which can limit it to zero possibilities if the non-matching strings are too inclusive.
txt2re does something like this.
How about the following (matches every string)?
.*
I think that Regulazy by Roy Osherove does this to a certain extent, or it may be Regulator. BOth are on this page:
http://weblogs.asp.net/rosherove/pages/tools-and-frameworks-by-roy-osherove.aspx
if your input strings are not random strings and they are based on some rules, by using a parser (i.e. jflex), you can create a regex generator which will generate a regex w.r.t. the given strings.
Look at txt2re.
This site holds a form that takes a sample string and generates a regex pattern that can match the given string.
Then it generates the corresponding script for the following languages: Perl, PHP, Python, Java, Javascript, ColdFusion, C, C++ Ruby, VB, VBScript, J#.net, C#.net, C++.net, VB.net