This regex pattern is not finding PageName correctly when pagename contains - like PAGE-NAME=HomePage-Left-Of-Calendar but it works when PAGE-NAME=HomePage can anyone help me to update this regex to support both...
var result = " <div id=\"header-left\" style=\"border:none\">";
result += "<!-- CMS-PAGE PAGE-NAME=HomePage-Left-Of-Calendar -->";
result += " </div>";
var pattern = #"(<!--)(\s*)(CMS-PAGE)(\s*)((PAGE-NAME)(=)(?<value>\w*)(\s*))*(\s*)(-->)";
var replaceTag = " <widget:ContentPageView runat=\"server\" PageName=\"##value\" Editable=\"True\">" +
"<DescriptionTemplate>...</DescriptionTemplate>" +
"</widget:ContentPageView>";
result = RegexReplaceWithUniqueTag(result, pattern, replaceTag);
The issue is a specific part of your regular expression:
(?<value>\w*)
This is matching only word characters (that's what \w means); however, you're also asking it to match non-word characters (specifically, the hyphen). Change this to:
(?<value>(\w|-)*)
and you will match both HomePage and HomePage-Left-Of-Calendar.
You need to add the hyphen (-) in ordered to be matched.
You can simply place both \w and - inside of a character class [] together which will match any character of word characters (a-z, A-Z, 0-9, _) and the literal character -
(?<value>[\w-]*)
Related
How do I say remove a number preceded by a non-digit and followed by a dash, but leave the preceding non-digit character?
RegExp: /[^\D]4\-/
String: http://localhost/images/4-6-.png
Remove: 4-
The 4- should be removed and it should leave the preceding / or -
This would work: /4\-/
But it would also remove 14- or 44-
Dynamic Code:
http://jsfiddle.net/flackend/8s9X9/2/
Static Code:
var category_id = 4;
var src = 'http://localhost/images/4-6-.png';
var regexp = new RegExp('[^\\D]'+ category_id +'\\-')
$('p').append('regexp: '+ regexp +'<br>');
$('p').append(src +'<br>');
src = src.replace(regexp, '');
$('p').append(src);
You want [\D] or [^\d], but not [^\D]. Regex is case-sensitive, \d matches a digit, and \D matches anything but a digit.
I need a regex which match everything expect for several words.
The input-string is something like:
This Is A <Test$gt;
It should match
This Is A Test
So I want to have everything around , < and >
I've tried something like [^ ] to ignore all appearances of but this excludes every character.
/&[a-zA-Z]{2,8};/g
Breakdown:
& - match & literally
[a-zA-Z]{2,8} - match any characters in ranges a-z and A-Z from 2 to 8 times
; - until a semi colon
The longest special character that you could encounter is ϑ - ϑ, and so I've taken this into account in the regex.
The proper formatting replaces each of the special characters with a space, and replaces multiple spaces in a row with a single space
let regex = /&[a-zA-Z]{2,8};/g,
string = "This Is A <Test>",
properlyFormatted = string.replace(regex, " ").replace(/\ +/g, " ");
console.log(properlyFormatted);
The alternative:
/&(?:lt|gt|nbsp);/g
Breakdown:
& - match & literally
(?:lt|gt|nbsp) - match any group in lt, gt, nbsp
; - directly followed by a semi colon
This regex will only take into account the specific characters you described.
let regex = /&(?:lt|gt|nbsp);/g,
string = "This Is A <Test>",
properlyFormatted = string.replace(regex, " ").replace(/\ +/g, " ");
console.log(properlyFormatted);
I want to keep only the last term of a string separated by dots
Example:
My string is:
abc"val1.val2.val3.val4"zzz
Expected string after i use regex:
abc"val4"zzz
Which means i want the content from left-hand side which was separated with dot (.)
The most relevant I tried was
val json="""abc"val1.val2.val3.val4"zzz"""
val sortie="""(([A-Za-z0-9]*)\.([A-Za-z0-9]*){2,10})\.([A-Za-z0-9]*)""".r.replaceAllIn(json, a=> a.group(3))
the result was:
abc".val4"zzz
Can you tell me if you have different solution for regex please?
Thanks
You may use
val s = """abc"val1.val2.val3.val4"zzz"""
val res = "(\\w+\")[^\"]*\\.([^\"]*\")".r replaceAllIn (s, "$1$2")
println(res)
// => abc"val4"zzz
See the Scala demo
Pattern details:
(\\w+\") - Group 1 capturing 1+ word chars and a "
[^\"]* - 0+ chars other than "
\\. - a dot
([^\"]*\") - Group 2 capturing 0+ chars other than " and then a ".
The $1 is the backreference to the first group and $2 inserts the text inside Group 2.
Maybe without Regex at all:
scala> json.split("\"").map(_.split("\\.").last).mkString("\"")
res4: String = abc"val4"zzz
This assumes you want each "token" (separated by ") to become the last dot-separated inner token.
Suppose I have an email address, 'abcdef#gmail.com'. I want to replace all the characters between 'a' and 'f' so the result would look like 'a****f#gmail.com'.
Trying to do this with a regex and replace
str.replace(/^(.*?)#/gi, '*');
But the results look like this
*gmail.com
Is there a way to do what I need?
This is not an answer to your actual question, but I'd like to challenge you that your idea is not a good one. It's best not to show how long an email address is by replacing the internal letters with the same number of *s. It's better to use a fixed number of *s.
You seem to be using javascript, which doesn't have lookbehind assertions, and capturing in this case may be simpler to understand too, so I'd do this to replace with a constant number of *s
str.replace(/^(.).*(.#)/, '$1***$2')
I'd use a replace with a callback, where the user middle part can be also replaced with *s:
var email = "abcdef#gmail.com";
document.write(email.replace(/^(.)(.*)(.#[^#]*)$/, function(m, g1, g2, g3) {
return g1 + g2.replace(/./g, "*") + g3;
}));
Here is how the "outer" /^(.)(.*)(.#[^#]*)$/ regex works:
^ - matches start of a string
(.) - Group 1: any first character
(.*) - Group 2: any characters up to the character before the last #`
(.#[^#]*) - Group 3: one character before the last #, then # and then any 0+ characters other than # up to...
$ - end of string
The .replace(/./g, "*") will just replace any character with *. And it will be done only on the Group 2.
The regex you suggested in the comment should also work.
/(?!^).(?=[^#]+#)/g matches any character but a newline that is not the first character ((?!^)) and that has 1+ characters other than # after it and a #.
var re = /(?!^).(?=[^#]+#)/g;
document.body.innerHTML = "fake#gmail.com".replace(re, "*");
Given a string like String a="- = - - What is your name?";
How to remove the leading equal, dash, space characters, to get the clean text,
"What is your name?"
If you want to remove the leading non-alphabets you can match:
^[^a-zA-Z]+
and replace it with '' (empty string).
Explanation:
first ^ - Anchor to match at the
begining.
[] - char class
second ^ - negation in a char class
+ - One or more of the previous match
So the regex matches one or more of any non-alphabets that are at the beginning of the string.
In your case case it will get rid of all the leading spaces, leading hyphens and leading equals sign. In short everything before the first alphabet.
$a=~s/- = - - //;
In Javascript you could do it like this
var a = "- = - - What is your name?";
a = a.replace(/^([-=\s]*)([a-zA-Z0-9])/gm,"$2");
Java:
String replaced = a.replaceFirst("^[-= ]*", "");
Assuming Java try this regex:
/^\W*(.*)$/
retrieve your string from captured group 1!
\W* matches all preceding non-word characters
(.*)then matches all characters to the end beginning with the first word character
^,$ are the boundaries. you could even do without $ in this case.
Tip try the excellent Java regex tutorial for reference.
In Python:
>>> "- = - - What is your name?".lstrip("-= ")
'What is your name?'
To remove any kind of whitespace, use .lstrip("-= \t\r\n").
In Javascript, I needed to do this and did it using the following regex:
^[\s\-]+
and replace it with '' (empty string) like this:
yourStringValue.replace(/^[\s\-]+/, '');