I want to replace the numbers in the string with _number.We have to fetch the numbers only that dont begin with a character and replace them with a underscore .
Requirement : I have a string, so while processing I want to replace constants with _Constant.
example string :"(a/(b1/8))*100"
output expected :"(a/(b1/_8))*_100"
Please suggest how to do this in asp.net code behind.
Thanks in advance.
You'll need a regular expression and the replace function:
var str = '(a/(b1/8))*100';
alert( str.replace(/([^a-zA-Z0-9])([0-9])/g, '$1_$2') );
So, what's going on?
The /'s mark the beginning and end of the regular expression (the tool best suited to this task).
[^a-zA-Z0-9] means "nothing which is a letter or a number".
[0-9] means "a digit".
Together, they mean, "something which is not a letter or a number followed by a digit".
The g at the end means "find all of them".
The () groups the regular expression into two parts $1 and $2
The '$1_$2' is the output format.
So, the expression translates to:
Find all cases where a digit follows a non-alphanumeric. Place a '_' between the digit and the non-alphanumeric. Return the result.
Edit
As an aside, when I read the question, I had thought that the JS function was the desired answer. If that is not the case, please read rkw's answer as that provides the C# version.
Edit 2
Bart brought up a good point that the above will fail in cases where the string starts with a number. Other languages can solve this with a negative lookbehind, but JavaScript cannot (it does not support negative lookbehinds). So, an alternate function must be used (a NaN test on substr( 0, 1 ) seems the easiest approach):
var str = '(a/(b1/8))*100';
var fin = str.replace(/([^a-zA-Z0-9])([0-9])/g, '$1_$2');
if( !isNaN( fin.substr( 0, 1 ) ) ) fin = "_" + fin;
alert( fin );
Same as cwallenpoole's, just in C# code behind
string str = '(a/(b1/8))*100';
str = Regex.Replace(str, '([^a-zA-Z])([0-9])', '$1_$2');
Updated:
string str = "(a/(b1/8))*100";
str = Regex.Replace(str, "([^a-zA-Z0-9]|^)([0-9])", "$1_$2");
Why not try regular expressions:
That is:
search for the regex: "[0-9]+" & replace with "_ + regex."
ie.
String RegExPattern = #"[0-9]+";
String str = "(a/(b1/8))*100";
Regex.Replace(str, RegExPattern, "_$1");
Source: http://msdn.microsoft.com/en-us/library/ms972966.aspx
Hope that helps ya some!
Related
I'm working with RegEx on Javascript and here is where I stuck.
I have a simple string like
<html><body><span style=3D"font-family:Verdana; color:#000; font-size:10pt;=
"><div><font face=3D"verdana, geneva" size=3D"2">http://72.55.146.142:8880/=
order003.png.zip,120</body></html>
all i need to do is write javascript which can replace all strings in with "<" and ">" symbol.
I wrote something like this -
var strReplaceAll = Body;
var intIndexOfMatch = strReplaceAll.indexOf( "<" );
while (intIndexOfMatch != -1){
strReplaceAll = strReplaceAll.replace(/<.*>/,'')
intIndexOfMatch = strReplaceAll.indexOf( "<" );
}
but the problem is if body contains -
test<abc>test2<adg>
it will give me -
test
only or if body contains like -
<html>test<abc>test2<adg>
it will give me nothing please let me know how i can get-
testtest2
as a final output.
Try this regex instead:
<[^>]+>
DEMO:
http://regex101.com/r/kI5cJ7/2
DISCUSSION
Put the html code in a string and apply to this string the regex.
var htmlCode = ...;
htmlCode = htmlCode.replace(/<[^>]+>/g, '');
The original regex take too much characters (* is a greedy operator).
Check this page about Repetition with Star and Plus, especially the part on "Watch Out for The Greediness!".
Most people new to regular expressions will attempt to use <.+>. They will be surprised when they test it on a string like This is a <EM>first</EM> test. You might expect the regex to match <EM> and when continuing after that match, </EM>.
But it does not. The regex will match <EM>first</EM>. Obviously not what we wanted.
/(<.*?>)/
Just use this. Replace all the occurrences with "".
See demo.
I'm trying to match characters before and after a symbol, in a string.
string: budgets-closed
To match the characters before the sign -, I do: ^[a-z]+
And to match the other characters, I try: \-(\w+) but, the problem is that my result is: -closed instead of closed.
Any ideas, how to fix it?
Update
This is the piece of code, where I was trying to apply the regex http://jsfiddle.net/trDFh/1/
I repeat: It's not that I don't want to use split; it's just I was really curious, and wanted to see, how can it be done the regex way. Hacking into things spirit
Update2
Well, using substring is a solution as well: http://jsfiddle.net/trDFh/2/ and is the one I chosed to use, since the if in question, is actually an else if in a more complex if syntax, and the chosen solutions seems to be the most fitted for now.
Use exec():
var result=/([^-]+)-([^-]+)/.exec(string);
result is an array, with result[1] being the first captured string and result[2] being the second captured string.
Live demo: http://jsfiddle.net/Pqntk/
I think you'll have to match that. You can use grouping to get what you need, though.
var str = 'budgets-closed';
var matches = str.match( /([a-z]+)-([a-z]+)/ );
var before = matches[1];
var after = matches[2];
For that specific string, you could also use
var str = 'budgets-closed';
var before = str.match( /^\b[a-z]+/ )[0];
var after = str.match( /\b[a-z]+$/ )[0];
I'm sure there are better ways, but the above methods do work.
If the symbol is specifically -, then this should work:
\b([^-]+)-([^-]+)\b
You match a boundry, any "not -" characters, a - and then more "not -" characters until the next word boundry.
Also, there is no need to escape a hyphen, it only holds special properties when between two other characters inside a character class.
edit: And here is a jsfiddle that demonstrates it does work.
I'm trying to split string name\|dial_num|032\|0095\\|\\0099|\9925 by delimiter | but it will skip \|.
I have found solution in this link: Javascript regexp that matches '.' not preceded by '\' (lookbehind alternative) but it skips \\| too.
The right result must be: [name\|dial_num,032\|0095\\,\\0099,\9925].
The rule is in case \\\| or \\\\\| or etc, | is still a valid delimiter but in case \\\\| or even more, it isn't.
Any help will be appreciate .
the usual workaround is to use match instead of split:
> s = "name\\|dial_num|032\\|0095\\\\|\\\\0099|\\9925"
"name\|dial_num|032\|0095\\|\\0099|\9925"
> s.match(/(\\.|[^|])+/g)
["name\|dial_num", "032\|0095\\", "\\0099", "\9925"]
As a side note, even if JS did support lookbehinds, it won't be a solution, because (?<!\\)| would also incorrectly skip \\|.
I challenged myself to use replace String method..
I got the right result using regex101.com (a popular online tester for PCRE, Javascript and Python regular expressions engines)
// input : name\|dial_num|032\|0095\\|\\0099|\9925
// regex : ([^\\|](?:\\\\)*)\| with global flag
// replacement : $1,
// output: name\|dial_num,032\|0095\\,\\0099,\9925 <= seams okey right !?
Test ..
var str = 'name\\|dial_num|032\\|0095\\\\|\\\\0099|\\9925';
str = str.replace(/([^\\|](?:\\\\)*)\|/g,'$1,');
console.log(str);
// > name\|dial_num,032\|0095\\,\\0099,\9925
I am clueless about regular expressions, but I know that they're the right tool for what I'm trying to do here: I'm trying to extract a numerical value from a string like this one:
approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^
Ideally, I'd extract the following from it: 12345678901234567890123456789012 None of the regexes I've tried have worked. How can I get the value I want from this string?
This will get all the numbers:
var myValue = /\d+/.exec(myString)
mystr.match(/assignment_group=([^\^]+)/)[1]; //=> "12345678901234567890123456789012"
This will find everything from the end of "assignment_group=" up to the next caret ^ symbol.
Try something like this:
/\^assignment_group=(\d*)\^/
This will get the number for assignment_group.
var str = 'approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^',
regex = /\^assignment_group=(\d*)\^/,
matches = str.match(regex),
id = matches !== null ? matches[1] : '';
console.log(id);
If there is no chance of there being numbers anywhere but when you need them, you could just do:
\d+
the \d matches digits, and the + says "match any number of whatever this follows"
Looking to have a recursive function that takes a string and removes the ending '[x]'. For example 'abc [1] [3]' needs to be 'abc [1]'. The string could also be 'abc [1] [5] [2]' and would need to be 'abc [1] [5]'.
I'm trying str.replace(/[\\\[\d\\\]]$/, '') but it only replaces the very last closing bracket and ignores everything else.
Any ideas?
You don't need the outer enclosing brackets. Try: str.replace(/\[\d\]$/, '');
If it is guaranteed that the string always contains a [number], you could just use substring and lastIndexOf:
str = str.substring(0, str.lastIndexOf('['));
Update: Or just add a test:
var index = str.lastIndexOf('[');
if(index > -1) {
str = str.substring(0,index);
}
\[\d+\]$
that should say any bracket followed by any number of digits followed by a bracket, all at the end of the string.
I say "should" because I'm still not as proficient at regex as I'd like to be, but in regexr (a nifty little AIR app for testing regular expressions), it seems to work.
EDIT:
Just in case anybody wants to play around with regexr, it's at http://gskinner.com/RegExr/desktop/. I have no affiliation with it, I just think it's a nice tool to have.
\[\d+\]([^]]*)$ works in Python and should work in Javascript. This allows for trailing bits after the [x], which are left behind. I believe that's why you weren't seeing the expected results, because you left trailing whitespace behind. Also note that I changed the regex to allow x to be any number of digits -- if that's not what you want, remove the +.
Here's the code:
import re
s = 'abc [1] [5] [2]'
while True:
new_s = re.sub(r'\[\d+\]([^]]*)$', r'\1', s)
if new_s == s:
break
s = new_s
print s
and the output:
abc [1] [5]
abc [1]
abc
/(.*)([\[].*[\]]\Z)/ should do it, you will need to do it using a match method, and it will provide two groups in an array, one with your required string, and the other with the ending in it.