How to create regex pattern - value between "$" character - regex

How to create regex pattern - value between "$" character.
e.g.
String:
" <tag key = $value$ /> "
I want get "value" string from this...

If I understand correctly, you're trying to get a string that is located between 2 dollar signs. The code (perl) should look similar to:
if ($str =~ /\$(\w)\$/)
$substr = $1;
of course, you can replace the \w sign with a pattern of your choosing...
EDIT:
if ($str =~ /\<tag key \= \$(\w)\$ \/\>/)
$substr =$1;

From my understanding you are looking to return exact word from a string.
i.e return is from this island is beautiful instead of returning this, island and is
If I am correct explaining your problem the regex you are looking for will be \bis\b
For more information visit this link, http://www.regular-expressions.info/wordboundaries.html

In javascript, you could is the following:
var theString = '<tag key = $value$ />';
var newString = theString.replace(/^.*\$(.*?)\$.*$/, '$1');
Or you could use the RegExp object to do something like:
var pattern=new RegExp('\\$(.*?)\\$');
var newString = pattern.exec(theString )[1];

Related

How to replace numbers with numbers separated with numeric separators?

Having a text contains numbers; which regex expression to use to add numeric separators?
The separator character will be an underscore. Starting from the end, for every 3 digits there will be a separator.
Example input:
Key: 100000,
Value: 120000000000
Expected output:
Key: 100_000,
Value: 120_000_000_000
You can use any popular regex flavor (Perl, Pcre, Python etc.)
(?<=\d)(?=(?:\d\d\d)+\b) will get the positions where to insert the underscore.
Then it is just a matter of injecting the underscore, which is a non-regex task. For instance in JavaScript it would be:
let regex = /(?<=\d)(?=(?:\d\d\d)+\b)/g
let inputstr = `Key: 100000,
Value: 120000000000`;
let result = inputstr.replace(regex, "_");
console.log(result);
And in Python:
import re
regex = r"(?<=\d)(?=(?:\d\d\d)+\b)"
inputstr = """Key: 100000,
Value: 120000000000""";
result = re.sub(regex, "_", inputstr)
print(result)
Regular expressions are used to find patterns in a string. What you do with the matches are language specific.
The regular expression pattern to find three numbers is pretty simple: /\d{3}/
Apply that expression to your specific language to retrieve the matches and build your desired output string:
Perl, using split and then join:
$string = "120000000000"
$new_string = join('_', (split /\d{3}/, $string))
# value of $new_string is: 120_000_000_000
PHP, using split and then join:
$string = "120000000000"
$new_string = implode ("_", preg_split("/\d{3}/", $string))
# value of $new_string is: 120_000_000_000
VB, using split and then join:
Dim MyString As String = "120000000000"
Dim new_string As String = String.Join(Regex.Split(MyString, "\d{3}"), "_")
'new_string value is: 120_000_000_000

Find all commas between two seperate characters in string

I have a substring that contains commas. This substring lives inside of another string that is a semi colon delimited list. I need to match the commas in that substring. The substring has a key field "u3=" in front of it.
Example:
u1=something;u2=somethingelse;u3=cat,matt,bat,hat;u4=anotherthing;u5=yetanotherthing
Regex so far:
(?<=u3)(.*)(?=;)
The regex i've been working on above matches everything between "u3" and the last ";" in the outerstring. I need to match only the commas in the substring.
Any guidance would be greatly appreciated.
You didn't specify language!
C#, VB (.NET):
Using an infinite positive lookbehind,
(?<=u3=[^;]*),
Java:
Using a variable-length positive lookbehind:
(?<=u3=[^;]{0,9999}),
PHP (PCRE), Perl, Ruby:
Using \G along with \K token:
(?>u3=|\G(?!^))[^,;]+\K,
Live demo
JavaScript:
Using two replace() methods (if you are going to substitute),
var s = 'u1=something;u2=somethingelse;u3=cat,matt,bat,hat;u4=anotherthing;u5=yetanotherthing';
console.log(
s.replace(/u3=[^;]+/, function(match) {
return match.replace(/,/g, '*');
})
)
Try to use this regex:
(?<=u3)[^;]+
The result is:
=cat,matt,bat,hat
If this was PHP I would do this:
<?php
$str = 'u1=something;u2=somethingelse;u3=cat,matt,bat,hat;u4=anotherthing;u5=yetanotherthing;';
$split = explode(';', $str);
foreach ($split as $key => $value) {
$subsplit = explode('=',$value);
if ($subsplit[0] == 'u3') {
echo $subsplit[1];
preg_match_all('/,/', $subsplit[1], $matches, PREG_OFFSET_CAPTURE);
}
}
var_dump($matches);

How to use regex to change occurance of complex strings?

For example,
Say I want to change
all occurances of <img src="https://www.blahblah.com/i" title="Bob" />
To simply
Bob
This is for vb.net
Basically there are plenty of such pattern in a big string. I want to change every one of them.
This is what I tried
Dim tdparking = New System.Text.RegularExpressions.Regex("\w* (<img.*title="")(.*)"" />")
After that I suppose I would need to do some substitution. But how?
How would I do so?
var str = '<img src="https://www.blahblah.com/i" title="Bob" />';
str = str.replace(/<img[^>]*title="(\w+)"[^>]*>/,"$1");
document.write(str);
You can use this regex: \<img[^\<\>]+title=\"([a-zA-Z]+)\"[^\<\>]+\/\> and return $1 of the matching pattern:
In PHP it should be:
preg_match_all('#\<img[^\<\>]+title=\"([a-zA-Z]+)\"[^\<\>]+\/\>#', $html, $matches);
var_dump($matches[1]);
Regards,
Everyone is making this waay to hard - Here it is in vb.net
Dim reg as Regex = New Regex("(?<=title="""").+(?="""")")
Dim str as String = "<img src=""https://www.blahblah.com/i"" title=""Bob"" />"
Dim match as String = reg.match(str).value
Depending on how the string is inputted you will either need
"(?<=title="""").+(?="""")" 'If there is Double quotes ("")
or
"(?<=title="").+(?="")" 'Or single quotes (")
Also there is no need for you to get downvoted - here is a point back

Convert regex string to URL

Im trying to convert words into URLs. the words are separated with a comma ,. But my problem is that the first word is not taken into account, because there is no comma infort of it.
public static function convertHashtags($str){
$regex = "/,+([a-zA-Z0-9_]+)/";
$str = preg_replace($regex, '$0', htmlentities($str));
return($str);
}
For example $str=june,mars,april results that only mars and april get URLed, not june.
You can change your regex to:
$regex = '/(?<=,|^)([a-zA-Z0-9_]+)/';
to match line start or comma before your words.
You can shorten your regex to:
$regex = '/(?<=,|^)(\w+)/';

Stripping out angle brackets with a regular expression

Having the following string:
<str1> <str2> <str3>
I am trying to do the regex such that I get the following 3 strings in C:
str1
str2
str3
I am trying to use the following regex but it doesn't seem to be yielding what I am aiming for:
<[a-zA-Z0-9]*.>
According to http://www.myregextester.com/index.php, that regex is going to yield
<str1>
<str2>
<str3>
How can I take out the <>'s?
Also, I'd also like to ONLY match strings with format, i.e., with 3 <>'s, no more, no less. How to approach that?
This can be easily done with a perl-compatible regular expression like this:
<([^>]+)>
You just have to make sure to tell your library to search for all matches, instead of trying to match the regular expression against the whole string. You will end up with str1, str2 and str3 as the first group match then.
What if you change it for <([a-zA-Z0-9]*.)>
<([a-zA-Z0-9]*?)> - Then you access the second match group of each match. (There is a PHP example on how to access the match group on http://www.myregextester.com/index.php
You can do this with positive lookaround ( http://www.regular-expressions.info/lookaround.html ) like this (?<=<)[a-zA-Z0-9]*(?=>) but not all flavours support it ( http://www.regular-expressions.info/refflavors.html )
Disclaimer: Don't copy Regex from the web if you do not understand how and why it works. This includes answers on Stack Overflow!
This regex would do the job:
/^<([a-zA-Z0-9]*?)>\s<([a-zA-Z0-9]*?)>\s<([a-zA-Z0-9]*?)>$/
example in Perl:
#!/usr/bin/perl
use 5.10.1;
use strict;
use warnings;
my $re = qr/^<([a-zA-Z0-9]*?)>\s<([a-zA-Z0-9]*?)>\s<([a-zA-Z0-9]*?)>$/;
my $str = q!<str1> <str2> <str3>!;
$str =~ s/$re/$1\n$2\n$3/;
say $str;
Output:
str1
str2
str3
You can simple use regex like given below to apply a check on angular brackets.
var text= "<span></span>";
var check = new RegExp(/[^>]\d+[a-z]*[^<])/ );
var valid = check text=
if(!valid) {
return false;
}
else
{
return true;
}