All Questions
Tagged with string-manipulation regular-expressions
54 questions
39
votes
1
answer
2k
views
Why is StringExpression faster than RegularExpression?
Edit: as noted by Albert Retey the performance difference is only seen when sub expression extraction is performed. If this test is used below the timings are similar:
...
27
votes
1
answer
637
views
Why Except is so slow as compared to equivalent RegularExpression?
When comparing performance of the solutions suggested for this question I discovered that StringExpression involving Except is ...
23
votes
1
answer
527
views
Can Mathematica convert a RegularExpression into a StringExpression or vice-versa?
I know that Mathematica can perform string comparison using either StringExpression or RegularExpression. Although regular ...
20
votes
4
answers
3k
views
Finding all dictionary words that can be made with a given set of characters (Wordfeud/Scrabble)
This is a slightly more involved variant of this recent question. By sheer coincidence I happened to have been building a Wordfeud (Scrabble clone) game in Mathematica.
My interactive Wordfeud board ...
19
votes
4
answers
757
views
Balanced Shortest[] and string patterns
The pattern Shortest["A"~~__~~"B"] is oriented : It assumes the text is read from left to right and it takes the text between the first ...
19
votes
2
answers
7k
views
Should I use \\\" or \" to match " in RegularExpression?
Both of the following return True:
...
14
votes
3
answers
564
views
Does RegularExpression support "(?R)"?
Mathematica's RegularExpression is based on Perl's PCRE library, which supports recursive regular expressions using (?R) (or, ...
14
votes
2
answers
487
views
How can one represent a string pattern with prohibited substrings?
Say I have the following string: "<hello>something WRONG</hello><hello>something else</hello>"
I am trying to find code that works like the ...
14
votes
1
answer
157
views
What types of string patterns can't be translated into regexes and hence do not work?
In a comment to related question Oleksandr R. made an interesting remark:
... because string patterns are converted into regular expressions and
matched using PCRE, they are subject to some ...
13
votes
6
answers
640
views
Wrong result from StringCases with Shortest
I have a list of strings like this:
...
13
votes
1
answer
4k
views
Mathematica equivalent of regex '\w{2,3}'
It beats me to write the Mathematica pattern equivalent to the regex:
\w{2,3}
Any ideas?
12
votes
2
answers
501
views
Convert recursive RegularExpression to StringExpression?
Consider the following expression for matching balanced brackets:
...
11
votes
4
answers
2k
views
How to get the longest bracket pairs from a string
In C#, we can use regular expression to do some balancing groups, like we can get
{<aa <bbb> <bbb> aa>, <<dfa>a>}
from
...
10
votes
3
answers
327
views
Extracting URLs from a list of strings
If I have a list of strings:
list = {"my element 1 http://link.com", "http://link2.com my element 2"}
then how do I extract a list of the valid ...
9
votes
4
answers
2k
views
How to convert a string having nested parentheses into an expression?
I am a Wolfram Language/Mathematica beginner and trying to convert STEP ISO10303 commands into geometrical regions.
A simple command looks like
...
9
votes
3
answers
2k
views
How remove specified substring and everything after it to the end of a string?
If I have, say, the string,
txt = "now is the time for all (good) men"
then I want to remove the substring beginning with "for" and ending at the end of the ...
9
votes
1
answer
184
views
Can we use named references on the RHS in StringReplace?
In this great answer WReach demonstrated some advantages of usage of Named Patterns inside of RegularExpression and described the supported syntax:
PCRE in ...
8
votes
4
answers
471
views
Dictionary lookups of words composed of specified characters
I am trying to find all English words composed of specified characters. For example, all words composed of one or more of the following: ...
8
votes
4
answers
630
views
RegEx replacement with backreference followed by a numeric literal
I have a text file which contains lines of the type:
SYMATTR InstName U1
where "1" can be actually a number with 1 to 3 digits.
I want to replace the above line with the following 2 lines:
...
8
votes
1
answer
6k
views
What pattern will match accented letters in a regular expression?
How should I modify the following such that the accented letter É is matched and the expression returns True?
...
6
votes
3
answers
688
views
Extract substring up to specific character non-inclusive
I was wondering if there is a way with StringCases to start at the beginning of a string and stop one character before a known character.
...
6
votes
1
answer
425
views
How to iterate over a list using a Regex
I need to replace all "Nx" with "N0x" in the following, but I can't seem to figure it out.
...
6
votes
1
answer
176
views
Regex named groups -- how to refer back to them in the replacement string [duplicate]
We can use group numbers to reuse the same piece of code while inside regex string, like that:
StringCases["x = y", RegularExpression["([\\w\\s]+)=(?1)"]]
<...
5
votes
4
answers
2k
views
Creating lists from a string, separating words from numbers
Say I have a string that contains numbers and words, such as this one:
string = "there are 1234 words and numbers 5678 in here $999";
How would I separate the ...
5
votes
4
answers
562
views
Regular Expression with +/-
Is it possible to extract the sign using Regular Expression ?
For example {"Today+100","Yesterday-200"}. And I need {"Today",100,"Yesterday",-200}.
Currently, I use Regular Expression to get the ...
5
votes
2
answers
243
views
using StringReplace and StringPattern to convert exp(...) to Exp[....]
I am looking at a file generated by another CAS. I'd like to convert it to Mathematica syntax.
Only issue is how to convert exp(....) to ...
5
votes
2
answers
1k
views
using regex to find a specific word
I want to find the word "circle" and "circles" in the list.
list = {"circle" , "picircle" , "circles" }
The regex ...
5
votes
3
answers
140
views
StartOfLine and EndOfLine not working well inside Shortest
Suppose that I want to replace lines containing "b" with a new "XXX" line. This works as expected with regular expressions:
...
5
votes
2
answers
290
views
StringCases with string replace and with escape characters
I want to extract url address of all css files inside HTML source.
First output is as expected. But why second and third outputs are different? Notice that only "href" was removed from ...
5
votes
1
answer
485
views
Use cases to select subelements by Regex
I have a nested list and would like to select a sub set of lists using a RegEx and I would prefer to use patterns (mainly for readability).
...
5
votes
2
answers
82
views
How to match numbers to alphabets with period and space,
ToCharacterCode["ABCD...XYZ"] - 64
I know that I can make a list of alphabets in this way.
However, I need to assign " " and <...
5
votes
3
answers
2k
views
Extract date and time from a natural language string
Is there a way to robustly extract date and time from a text? Say for example:
...The event will be held on 17/10/2013 at 3:30 PM in the room 121 and
everyone is welcome...
should return:
...
4
votes
5
answers
743
views
A simple StringCases problem
Given this text:
text1 = " A Vitamin D Deficiency (ICD-9-CM 268.9) (ICD-9-CM 268.9) 09/11/2015 01 ";
Yes, the ICD code values are duplicates- possibly but ...
4
votes
3
answers
157
views
Extract the name of a point with Regular Expression
I have a list of points as strings in the form
{"A1=(345.2345,3423.1)","B=(2123,97.123)","KX=(2144.546,-4455)"}
and I would like to extract the names using ...
4
votes
2
answers
356
views
Using parenthesized Regex backreferences in StringReplace
(I am sorry for my poor English,I'll try to use a simple sentence to convey me.)
I can use StringCases to get the target to change like this
...
4
votes
1
answer
435
views
Extracting integers from a string
Imagine I have a block of text that contains words and numbers. I want to extract all the integers from the text. Thus, if the input is
...
3
votes
4
answers
960
views
how to extract the string using regular expressions
I have the list :
a1 = {"struct name1 {","struct name2{","struct name3{","struct name4{",
"struct name5{","struct lastStruct{"}
I tried to extract the ...
3
votes
2
answers
147
views
Small trouble with variable referencing in StringReplace?
I am trying to do the following:
Take a string abcdEFGpcf and replace whatever is between abcd and ...
3
votes
2
answers
401
views
Please help translate regular expression to StringExpression
I am familiar with regular expressions in other languages (e.g., Perl, Python, ViM), but the Mathematica StringExpression is baffling me.
I'd like to match strings ...
3
votes
3
answers
127
views
Extract doi from a text string
I have a large list with bibliometric information including the doi like this example:
...
3
votes
2
answers
219
views
Mathematica regex equvalent of \l
In normal RegEx \l will make the character to the right lowercase. However, it seems like the RegEx in Mathematica does not have this functionality.
For example
<...
3
votes
1
answer
90
views
Convert an expression's symbols of form a$x to a[x]
I want a function that takes expressions of the following form: a$x and converts them to a[x].
In particular, consider the ...
2
votes
1
answer
131
views
StringExpression: pattern reapeted one or more times matches pattern repeated zero times
I try to construct patterns using Riffle and Except
StringExpression@Riffle[{"a", "b", "c"}, Except[{"=", ","}] ..]
and expect it to match "a_b_c", but not "a_bc"...
2
votes
2
answers
78
views
Retain or delete parts of textstring in tweets
For a twitter analysis I collected many tweets. For example:
...
2
votes
1
answer
182
views
How to refer to a character group without ambiguity in a regex?
Suppose I want to realize a replacement in a string: "aabbcc11dd ee" -> "aabbcc112dd ee", I can use a regex like this within ...
2
votes
1
answer
126
views
Make MyLogicalExpand
The function by Mathematica to expand a formulation with ANDS, ORS, NOT, XOR, and others, is LogicalExpand, let see an example:
...
1
vote
2
answers
293
views
text analysis: split document in separated lines
For a text analysis I want to split a large document in separated lines. Then I want to calculated for example the average number of words. A example of the document is the following:
...
1
vote
1
answer
115
views
Are all of the numbers in my file legitimate hex numbers
I have about 10,000 hexadecimal numbers (32 digits each) in an excel file, in one column. I want to verify that all of them are indeed hexadecimal number and that no other characters are present. My ...
1
vote
1
answer
74
views
Difficulty using StringCases to obtain desired substring
I have extracted and stored some plaintext strings (derived from a pdfs) from the web that I wish to search for various values following certain text substrings that are different for different ...
1
vote
0
answers
41
views
A successful StringReplace with an error message [closed]
An example of StringReplace and RegularExpression :
...