Regular Expression for BBcode replacement - regex

I need PCRE regex for replace
This
[quote author=MEMBER link=topic=8.msg1111 date=1438798587]
sample text[/quote]
To this one
[quote="MEMBER, post: 1111"]sample text[/quote]
So i need:
Delete attribute date=xxxxxxxxxx and place " in end of tag (after post id)
Replace link=topic=8.msg to post:
Replace author= to ="
Can sombody help please?
Thanks!

Well, you can try this :
\[quote\sauthor=([^\s]+)\s*.*?msg(\d+)\s[^]]*]\s*([^[]*)(\[\/quote])
See this demo.
Although I can't be sure this is really standard to your kind of inputs since you only provided one sample.

Related

RegEx remove part of string and and replace another part

I have a challenge getting the desired result with RegEx (using C#) and I hope that the community can help.
I have a URL in the following format:
https://somedomain.com/subfolder/category/?abc=text:value&ida=0&idb=1
I want make two modifications, specifically:
1) Remove everything after 'value' e.g. '&ida=0&idb=1'
2) Replace 'category' with e.g. 'newcategory'
So the result is:
https://somedomain.com/subfolder/newcategory/?abc=text:value
I can remove the string from 1) e.g. ^[^&]+ above but I have been unable to figure out how to replace the 'category' substring.
Any help or guidance would be much appreciated.
Thank you in advance.
Use the following:
Find: /(category/.+?value)&.+
Replace: /new$1 or /new\1 depending on your regex flavor
Demo & explanation
Update according to comment.
If the new name is completely_different_name, use the following:
Find: /category(/.+?value)&.+
Replace: /completely_different_name$1
Demo & explanation
You haven't specified language here, I mainly work on python so the solution is in python.
url = re.sub('category','newcategory',re.search('^https.*value', value).group(0))
Explanation
re.sub is used to replace value a with b in c.
re.search is used to match specific patterns in string and store value in the group. so in the above code re.search will store value from "https to value" in group 0.
Using Python and only built-in string methods (there is no need for regular expressions here):
url = r"https://somedomain.com/subfolder/category/?abc=text:value&ida=0&idb=1"
new_url = (url.split('value')[0] + "value").replace("category", 'newcategory')
print(new_url)
Outputs:
https://somedomain.com/subfolder/newcategory/?abc=text:value

How to extract FirstName and LastName from html tags with regex?

I have response body which contains
"<h3 class="panel-title">Welcome
First Last </h3>"
I want to fetch 'First Last' as a output
The regular expression I have tried are
"Welcome(\s*([A-Za-z]+))(\s*([A-Za-z]+))"
"Welcome \s*([A-Za-z]+)\s*([A-Za-z]+)"
But not able to get the result. If I remove the newline and take it as
"<h3 class="panel-title">Welcome First Last </h3>" it is detecting in online regex maker.
I suspect your problem is the carriage return between "Welcome" and the user name. If you use the "single-line mode" flag (?s) in your regex, it will ignore newlines. Try these:
(?s)Welcome(\s*([A-Za-z]+))(\s*([A-Za-z]+))
(?s)Welcome \s*([A-Za-z]+)\s*([A-Za-z]+)
(this works in jMeter and any other java or php based regex, but not in javascript. In the comments on the question you say you're using javascript and also jMeter - if it is a jMeter question, then this will help. if javaScript, try one of the other answers)
Well, usually I don't recommend regex for this kind of work. DOM manipulation plays at its best.
but you can use following regex to yank text:
/(?:<h3.*?>)([^<]+)(?:<\/h3>)/i
See demo at https://regex101.com/r/wA2sZ9/1
This will extract First and Last names including extra spacing. I'm sure you can easily deal with spaces.
In jmeter reg exp extractor you can use:
<h3 class="panel-title">Welcome(.*?)</h3>
Then take value using $1$.
In the data you shown welcome is followed by enter.If actually its part of response then you have to use \n.
<h3 class="panel-title">Welcome\n(.*?)</h3>
Otherwise above one is enough.
First verify this in jmeter using regular expression tester of response body.
Welcome([\s\S]+?)<
Try this, it will definitely work.
Regular expressions are greedy by default, try this
Welcome\s*([A-Za-z]+)\s*([A-Za-z]+)
Groups 1 and 2 contain your data
Check it here

regular expression : get super scripted text

I would like to get super scripted text via following html string.
testing to <sup>supers</sup>cript o<sup>n</sup>e
The result I would like to get is like below
supers
n
This is what I tried right now
But the result is not what I want.
<sup>supers
<sup>n
Could anyone give me suggestion please?
You can use lookbehind in your regex:
(?<=<sup>)[^<]*
Update Demo
Use this if there may be other HTML tags between <sup> and </sup>:
(?<=<sup>)(.*?)(?=<\/sup>)
Check the demo.
You were close, just not capturing your match:
Updated regex
(?:<sup>)([^<]*) I just added a capture group around your match
(?<=<sup>)([^<]*?)(?=<\/)
This should work.
See demo.
http://regex101.com/r/sA7pZ0/13

RegExp replace after

I have some link templates and I need to replace substrings inside of that links.
Link templates:
"/all_news"
"/all_news/"
"/all_news/page1"
"/all_news/page1/"
All of these templates mean the same thing - first page of news page without filtering.
So I need to:
1st template - insert "/pageX"
2nd template - insert "pageX"
3rd and 4th templates - replace page number
Is it possible with only one regexp?
If yes, then please help me.
If no, then I have 2nd question:
maybe its possible to replace everything after "/all_news" on "/pageX"?
I mean next logic:
string started
ok, I see substring "/all_news"
I replace everything after "/all_news" even if nothing exist(if string ends by "/all_news")
I return "/all_news/pageX".
This'll do it.
'/all_news/page1'.replace(/(.*\/all_news).*/,'$1' + '/pageX');
Just one for all.
Java has lookbehind. It negates the need for the $1. The solution looks like:
String result = "/all_news/page1";
String pattern = "(?<=\\/all_news).*";
System.out.println(result.replaceAll(pattern,"/PageX"));
Cheers.

How do I escape an apostrophe in my XPath text query with Perl and Selenium?

I have an XPath query which needs to match some text in a span attribute, as follows:
my $perl_query = qq(span[text\(\)='It's a problem']);
$sel->click_ok($perl_query);
Where the text has no apostrophe there is no problem.
I've tried the following instead of 'It's a problem':
'It\'s a problem'
'It&apos\;s a problem'
'It\${apos}s a problem' #some thread on Stackoverflow suggested that this was a solution implemented by Selenium, but it doesn't work.
Any ideas?
On a different note, if I can't solve this, I'd be happy enough matching 'a problem' but not sure how to do regex matching in XPath with Selenium.
Thanks for any pointers
It's an XPath problem rather than the Perl problem.
The problem was discussed and answered here in great detail:
http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes (broken link)
In a nutshell, modify your xquery to assemble the quote-containing string using concat()
my $perl_query = qq(span[text\(\)=concat("It","'","s a problem"]);
A couple of suggestions; hopefully at least one of them will work:
my $perl_query = qq!span[text()='It\\'s a problem']!;
my $perl_query = qq!span[text()="It's a problem"]!;
I just had the same problem and google didn't give me a satisfied solution.
I tried to substring this: value=' - ending with an Apostrophe.
My XPath that works look like:
"substring-after(., concat('value=', ''''))"
So four Apostrophes in a row.
Well the post is quite old. But here goes my working answer for those who still come wandering around looking for escaping single apostrophe and unable to find proper answer.
Text = It's a problem
Solution xpath = //div[text()=\"It's a problem\"]
or
Solution xpath = //div[contains(text(),\"It's a\")]
Is it possible that the actual text on the web page is a curly quote and not a straight apostrophe? Also, you may have extra space at the beginning and end of the span, so that the strict equality against your string won't match.
Consider breaking up your string if possible:
my $spanValue = q/text()='It's a problem'/;
my $perlQuery = qq/span[$spanValue]/;
# $perlQuery = span[text()='It's a problem']
The solution to escaping apostrophes in xpath string literals is to double the apostrophe, e.g.
qq(span[text()='It''s a problem'])