Still need help with regex (splitting string)

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hey everyone,

I still don't get on how to work with regex and get content from a string.

This is a string I am using:

25. Apples 25 This String(1)14/02/2011

The string I want from the above string is:

This String(1)

Note that the first string is an example and I have more strings like this:

24. Lemons 24 First String(1)14/02/2011
25. Apples 25 Second String(2)14/02/2011
26. Chocolat 26 Third String14/02/2011
27. Nougat 26 Fourth String-

These strings above will show the following output:

First String(1)
Second String(2)
Third String
Fourth String

I hope someone can help me.

Tomas
 
I don't think you can achieve this with regex. Seeing your example, you could first search for the 3rd space in the string (indexof) and then use a substring based on the resulting value. Afterwards you could search for the '(' after the 'string' word. If found, substring till the ')', if not found, substring till 'string').
 
Upvote 0
Here:
Code: Copy
Sub Activity_Create (FirstTime As Boolean)
   Dim s() As String
   s = Array As String("24. Lemons 24 First String(1)14/02/2011", _
      "25. Apples 25 Second String(2)14/02/2011", _
      "26. Chocolat 26 Third String14/02/2011", _
      "27. Nougat 26 Fourth String-")
   For i = 0 To s.Length - 1
      Log(SplitString(s(i)))
   Next
End Sub
Sub SplitString(s As String) As String
   Dim m As Matcher
   m = Regex.Matcher("\d\d ([A-Za-z]+ [A-Za-z]+(\(\d\))?)", s)
   m.Find
   Return m.Group(1)
End Sub
 
Upvote 0
Code: Copy
Sub getstring(st As String) As String
Dim str1(5),str2(2) As String
str1 = Regex.Split(" ",st)
str2 = Regex.Split(")",str1(4))
If str2.length > 1 Then 
   Return str1(3) & " " & str2(0) & ")"
Else
   Return str1(3) & " " & "String"
End If
End Sub

After I wrote it - I saw that it is similar to what COBRASoft proposed :)
 
Last edited:
Upvote 0
Erel, nice found! Your solution depends on the 2nd number in the string, mine depends on the amount of spaces in front of the required string.

XverhelstX, honestly, I find both solution quite risky. If something is changing in the base string, e.g. a number of 2 digits in the product or a space, these solutions won't work anymore.
 
Upvote 0
Hi guys, i was wondering if you could help me with my problem i have an xml string that i need to get information from, i've tried using the xml library but i really don't understand how to use it, so i was wondering if it is possible to accomplish what i want using the regex pattern, this is the string i need to get data from:

<result>
<rep name="Brad Sherman" party="D" state="CA" district="27" phone="202-225-5911" office="2242 Rayburn House Office Building" link="http://bradsherman.house.gov/"/>
<rep name="Howard Berman" party="D" state="CA" district="28" phone="202-225-4695" office="2221 Rayburn House Office Building" link="http://www.house.gov/berman/"/>
<rep name="Barbara Boxer" party="D" state="CA" district="Junior Seat" phone="202-224-3553" office="112 Hart Senate Office Building" link="http://boxer.senate.gov"/>
<rep name="Dianne Feinstein" party="D" state="CA" district="Senior Seat" phone="202-224-3841" office="331 Hart Senate Office Building" link="http://feinstein.senate.gov"/>
</result>
i need to get the rep name and the district
can you guys look at this and help me figure it out please.

Thanks,
Walter
 
Upvote 0
If st holds the string then:

Dim str(15), name, district As String
str = Regex.Split(chr(34),st) ' chr(34) is "
name = str(1)
district = str(7)
 
Upvote 0
If st holds the string then:

Dim str(15), name, district As String
str = Regex.Split(chr(34),st) ' chr(34) is "
name = str(1)
district = str(7)

You will also need to trim off the trailing " from the string.
 
Upvote 0
XML parsing

thanks guys, however i had to do a minor change instead of using chr(34) i had to use chr(39) which is a single quote, for some reason once i get the response from the api the string shows single quotes instead double quotes ("") but your suggestion worked, now, each district has 1 or more representatives depending on the size of the district, how can i retrieve all representatives for a given district, in the example above there's 2 representatives, how would i retrieve both names and both district numbers?

any ideas?

thanks for your help
 
Upvote 0

Similar Threads

  • Question Question
Android Question Regex B4A & B4J
Replies
22
Views
10K
  • Question Question
Android Question Regex.split
Replies
4
Views
3K
  • Question Question
Android Question Regex problem
Replies
18
Views
8K
  • Question Question
Android Question Regex split
Replies
2
Views
4K
Back
Top